Taro实现多个input双向绑定数据

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双向绑定

class Index extends Component {

	constructor (props) {
		super(props)
		this.state = {
			userName: '',
			password: ''
		}
	}
	
	*通过onChange事件来更新state的userName*
	handleChane = (inputName, e) => {
		// tarojs的e一定要放在后面
		this.setState({
			[userName]: e.target.value
		})
	}
	
	handleClick () {
		console.log(this.state)
	}
	
	render () { 
		retrun (
			
			
			
		)
	}
	
}

你可能感兴趣的:(React,Taro,reactjs)