I don’t understand why my handleChange() function isn’t working. As soon as I start typing anything into the input text box, my whole application crashes. My submitMessage() function uses the same syntax, but it seems to work fine. I’ve tried re-writing my function as to simply pass an object to setState instead of a function, and that works fine. However, I want to know what exactly is wrong with my implementation here. I would appreciate any advice. Thank you!
class DisplayMessages extends React.Component {
constructor(props) {
super(props);
this.state = {
input: '',
messages: []
}
this.handleChange = this.handleChange.bind(this);
this.submitMessage = this.submitMessage.bind(this);
}
handleChange(event){
this.setState((state,props)=>{
return {
input: event.target.value,
messages: state.messages
}
});
}
submitMessage(){
this.setState((state,props)=>{
return {
input: '',
messages: [...state.messages,state.input]
}
});
}
render() {
let temp=this.state.messages.map(
(i) => {
return <li key={i}>{i}</li>
}
)
return (
<div>
<h2>Type in a new Message:</h2>
<input onChange={this.handleChange} value={this.state.input}/>
<button onClick={this.submitMessage}>Click</button>
<ul>
{temp}
</ul>
</div>
);
}
};
**Your browser information:**
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36
Challenge: Manage State Locally First
Link to the challenge: