reactjs - React updating state within the same component -
i new react, thought setting state re-render react component. did miss here, text not displayed until call this.forceupdate()
.
export default class hello extends react.component { constructor(props){ super(props); this.state = { value: ''}; this.handlechange = this.handlechange.bind(this); } render() { return ( <div> <input type="text" onchange={this.handlechange} /> <p> entered {this.state.value} </p> </div> ); } handlechange(event){ this.state = { value: event.target.value }; this.forceupdate(); } }
you should call .setstate
method instead of assign new value this.state
handlechange(event){ this.setstate({ value: event.target.value }) }
Comments
Post a Comment