Hi,
I managed to write the code and it looks like working but it doesn’ t pass a test saying that " The input element should render the value of input in local state".
Can someone explain me what is wrong?
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);
}
// Add handleChange() and submitMessage() methods here
handleChange (event){
this.setState(
{input: event.target.value})
}
submitMessage() {
let arr = [...this.state.messages,this.state.input];
this.setState({
messages: arr,
input: ""});
document.querySelector('input').value= ""; }
render() {
return (
<div>
<h2>Type in a new Message:</h2>
{ /* Render an input, button, and ul below this line */ }
<input type="text" onChange={this.handleChange} />
<button onClick={this.submitMessage} >Add message</button>
<ul>
{this.state.messages.map(msg => <li>{msg}</li>)}
</ul>
{ /* Change code above this line */ }
</div>
);
}
};