【react】input输入框可输入的最好实现方式

使用的是refs。react中输入框不能直接定义value。输入框是可变的,react会提示报错。须要使用的inChange事件(输入框内容被改变时触发)。react

要定义输入框初始值,须要在componentDidMount中定义,不能在componentWillMount中定义,由于render以后才能取到refs的input。使用this.refs.input1.value="初始值"。性能

改变输入框内容时,不会触发render重渲染。性能比更新state好。this

class Input extends React.Component{ componentDidMount(){ this.refs.input1.value="初始值" } change(){ console.log(this.refs.input1.value) } render(){ return ( <div className="customForm">
                <input type="text" ref="input1" onChange={this.change.bind(this)} />
            </div>
 ) } }
相关文章
相关标签/搜索