I've got some confusion with React's event handler
I have a component like this, with handleChange handling onChange event:
var SearchBar = React.createClass({
getInitialState(){
return {word:''};
},
handleChange: function(event){
this.setState({word:event.target.value});
alert(this.state.word);
},
render: function () {
return (
<div style={{width:'100%',position:'0',backgroundColor:'darkOrange'}}>
<div className="header">
<h1>MOVIE</h1>
</div>
<div className="searchForm">
<input className="searchField" onChange={this.handleChange}
value={this.state.word} type="text" placeholder="Enter search term"/>
</div>
</div>
);
}
});
It does work, but not the way I expect. In textfield, when I type the first character, it alerts empty string, when the second character is typed, it alerts a string with only first character, and so on, with string length of n, it alerts the string with n-1 length
What did I do wrong here? How should I fix it?