Taro 遵循 React 语法规范,它采用与 React 一致的组件化思想,组件生命周期与 React 保持一致,同时支持使用 JSX 语法,让代码具有更丰富的表现力。
由于知道React是没有双向绑定的,所以每次要使用input输入框的时候,都需要写个方法去更新this.state。
代码示例
class Index extends Component {
constructor (props) {
super(props)
this.state = {
userName: ''
}
}
*通过onChange事件来更新state的userName*
handleChane = (e) => {
this.setState({
userName: e.target.value
})
}
handleClick () {
console.log(this.state.userName)
}
render () {
retrun (
<Input type='test' value={userName} onChange={this.handleChane.bind(this)} />
<button onClick={this.handleClick}>查看更新后的state</button>
)
}
}
多个Input双向绑定
class Index extends Component {
constructor (props) {
super(props)
this.state = {
userName: '',
password: ''