Tell us what’s happening:
I feel like I followed instructions and went back and forth assessing my own code against the answer solution trying to figure out why my code is throwing errors.
It has the same functionality, just written differently.
Any help would be greatly appreciated!
Your code so far
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){
const{name, value} = event.target;
this.setState({[name]:value});
//console.log(value);
}
submitMessage(event){
const{name, value} = event.target;
this.setState({[name]:this.state.messages.concat(value)});
this.setState({input:""});
//console.log(this.state.messages);
}
render() {
let arrMessages = this.state.messages.map((ele,index)=>{
return <li key={index}>{ele}</li>
})
return (
<div>
<h2>Type in a new Message:</h2>
{ /* render an input, button, and ul here */ }
<input
value={this.state.input}
name="input"
placeholder="Please type something"
onChange={this.handleChange}
/> Your input : {this.state.input}
<br/>
<button
onClick={this.submitMessage}
name="messages"
value={this.state.input}>Submit</button>
<br/>
<ul>{arrMessages}</ul>
{ /* change code above this line */ }
</div>
);
}
};
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36
.
Challenge: Manage State Locally First
Link to the challenge: