1、获取元素
import React,{Component} from 'react'
export default class UserAdd extends Component{
constructor(){
super();
}
handleSubmit=()=>{
let name=this.refs.name.value;//获取定义好的ref
console.log(name);
}
render(){
return(
<form onSubmit={this.handleSubmit}>
<div className="from-group">
<label htmlFor="name">姓名</label>
<input type="text" className="form-control" ref="name"/>//先给要获取的dom定义一个ref
</div>
<div className="from-group">
<input type="submit" className="btn btn-primary"/>
</div>
</form>
)
}
}
2、setState同步更新
setState函数的第二个参数允许传入回调函数,在状态更新完毕后进行调用
this.setState({
load: !this.state.load,
count: this.state.count + 1
}, () => {
console.log(this.state.count);
console.log('加载完成')
});