您可以通过两种方式在React应用中获取输入更改.
一个是通过使用
<input type="text" onChange={this.handleChange} />
Run Code Online (Sandbox Code Playgroud)
另一个是
<form onChange={this.handleChange} onSubmit={this.handleChange} />
...
</form>
Run Code Online (Sandbox Code Playgroud)
当你应该使用第一个和另一个时.
在react-native中考虑TextInput,下面的实现似乎是一种标准方法:
<TextInput
value={this.state.text}
onChangeText={text => this.setState({ text })}
/>
Run Code Online (Sandbox Code Playgroud)
但是,setState()是一个异步函数.我想知道是否可能由于setState()的执行顺序而遇到不一致.
例如:
我有与创建的应用程序React 16.3.X
,需要this.props
在getDerivedStateFromProps()
方法。
例如:
static getDerivedStateFromProps(nextProps, prevState) {
console.log(this.props); // `this` is not defined
if (nextProps.id !== this.props.id) {
// do something...
}
return null;
}
Run Code Online (Sandbox Code Playgroud)
显然this
未在规定static
的方法,但我需要知道的是有没有办法让this.props
在getDerivedStateFromProps()
?