Edit: I don’t want to call handleChange only if the button has been clicked. It has nothing to do with handleClick. I gave an example in the @shubhakhatri answer’s comment.
I want to change the input value according to state, the value is changing but it doesn’t trigger handleChange()
method. How can I trigger handleChange()
method ?
class App extends React.Component { constructor(props) { super(props) this.state = { value: 'random text' } } handleChange (e) { console.log('handle change called') } handleClick () { this.setState({value: 'another random text'}) } render () { return ( <div> <input value={this.state.value} onChange={this.handleChange}/> <button onClick={this.handleClick.bind(this)}>Change Input</button> </div> ) } } ReactDOM.render(<App />, document.getElementById('app'))
Here is the codepen link: http://codepen.io/madhurgarg71/pen/qrbLjp