Data binding in React
Data binding is a process of binding a UI component like textbox and code behind the object. For example, your input is:
<div className="post_input">
<input className='post_data_input_overlay' placeholder="Ask your question here" ref="postTxt"/>
</div>
Data binding can be done using controlled input. Controlled input is gained by binding value to a state variable and onChange event to change the state as the input value changes.
class App extends React.Component {constructor() {
super();
this.state = {value : ''}
}
handleChange = (e) =>{
this.setState({value: e.target.value});
}
render() {
return (
<div>
<input type="text" value={this.state.value} onChange={this.handleChange}/>
<div>{this.state.value}</div>
</div>
)
}
}
ReactDOM.render(<App/>, document.getElementById('app'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.js"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.js"></script>
<div id="app"></div>
Comments
0 comments
Please Sign in or Create an account to Post Comments