Why not change the component state?

Hello. There's a text input that renders the component TestInput.

class TestInput extends React.Component {

constructor(props){

super(props);

 this.state = {myValue: ""};

 this.onChangeHandler = this.onChangeHandler.bind(this); 
}

 onChangeHandler(e) {

 this.setState = ({
 myValue: e.target.value
});

//alert(e.target.value);
}

 render() {

return(

 <input classname="test-input" type="text" value="{this.state.myValue}" onchange="{this.onChangeHandler}" placeholder="enter a value">
);
}
}


When you change input - and triggered onChangeHandler method, which should change the state of an item. But to print anything in the text box is not possible. In the inspector myValue not change after lines with changing values - debugger jumping into react-dom.development.js. How to be? :with
June 14th 19 at 20:57
1 answer
June 14th 19 at 20:59
Solution
try this:

onChangeHandler (e) {
this.setState({
 myValue: e.nativeEvent.target.value
})
}


onChange={this.onChangeHandler}
Replace with:
onChange={this.onChangeHandler.bind(this)}
My God, I'm an idiot. Imagine, only after your comment and noticed that I wrote

this.setState = ({
myValue: e.target.value
});

Instead

this.setState({
myValue: e.target.value
});

Thank you very much! - stacey.Prosacco commented on June 14th 19 at 21:02

Find more questions by tags React