My goal with Formik was to create a scalable, performant, form helper with a minimal API that does the really really annoying stuff and leaves the rest up to you.
Formik 使用以下3个event handlers
handleChange, handleBlur, and handleSubmit
handleChange and handleBlur work exactly as expected–they use a name or id attribute to figure out which field to update.
也就是说Formik需要使用name或者是id来确认是哪个field被change了
以下两种方式使用Formik都是可以的
There are two ways to use Formik:
withFormik(): A Higher-order Component (HoC) that accepts a configuration object
Internationalize React apps. This library provides React components and an API to format dates, numbers, and strings, including pluralization and handling translations.
react intl 用于格式化日期, 数字, 字符串多元化和翻译(实际上可以用做国际化)
安装
$ npm install react-intl --save
import React, {Component} from 'react';
import ReactDOM from 'react-dom';
import {IntlProvider, FormattedMessage} from 'react-intl';
class App extends Component {
constructor(props) {
super(props);
this.state = {
name : 'Eric',
unreadCount: 1000,
};
}
render() {
const {name, unreadCount} = this.state;
return (
<p>
<FormattedMessage
id="welcome"
defaultMessage={`Hello {name}, you have {unreadCount, number} {unreadCount, plural,
one {message}
other {messages}
}`}
values={{name: <b>{name}</b>, unreadCount}}
/>
</p>
);
}
}
ReactDOM.render(
<IntlProvider locale="en">
<App />
</IntlProvider>,
document.getElementById('container')
);
const MyPoint = class Point{
get myname() {
console.log("getter:"+this._myname)
return this._myname;
}
set myname(value) {
console.log("setter:"+value)
this._myname = value;
}
};
var p = new MyPoint();
p.myname = "Lizhe"
p.myname
const map = new Map(
[
[1,"number 1"],
[2,"number 2"]
]
);
function strMapToObj(strMap){
let obj = Object.create(null);
for(let [k,v] of strMap){
obj[k] = v
}
return obj
}
console.log(strMapToObj(map));
4. 对象转Map
let obj = {
a:1,
b:2,
c:3
}
function objToStrMap(obj){
let map = new Map();
for(let k of Object.keys(obj)){
map.set(k, obj[k]);
}
return map;
}
console.log(objToStrMap(obj));
5. Map 转 JSON 对象
Map转为JSON要区分两种情况, 如果Map的键名都是字符串, 这时可以选择转为对象JSON
const map = new Map([[1, "number 1"], [2, "number 2"]]);
function strMapToObj(strMap) {
let obj = Object.create(null);
for (let [k, v] of strMap) {
obj[k] = v;
}
return obj;
}
function strMapToJson(strMap) {
return JSON.stringify(strMapToObj(strMap));
}
console.log(strMapToJson(map))
6. JSON 转为 Map
function objToStrMap(obj) {
let map = new Map();
for (let k of Object.keys(obj)) {
map.set(k, obj[k]);
}
return map;
}
function jsonToStrMap(jsonStr) {
return objToStrMap(JSON.parse(jsonStr));
}
let jsonStr = '{"name":"lizhe"}';
console.log(jsonToStrMap(jsonStr));
function test(){
let value = "hello world";
var promise = new Promise(
function(resolve,reject){
if(true){
resolve(value)
}else{
reject(value)
}
}
);
}
test();