Tell us what’s happening:
Hi, I’m having an odd problem. The only test I’m not passing is " Calling the method handleChange
should update the input
value in state to the current input."
The weird thing is that, as you can see, I’m logging these values in the console and to me it looks like the test should pass. The values match up after every keystroke in the input field. Also strange is that I’m pretty sure I should not be passing the test for rendering <li>
elements because that’s not rendered correctly in the test window, but it shows me passing that test anyway.
I would love any help explaining why I don’t pass the handlechange test, why I do pass the list test, or how I can fix my list so that it really does render properly (instead of just passing the test while being broken).
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) {
console.log(event.target.value);
this.setState(Object.assign(this.state, {input: event.target.value}));
console.log(this.state.input);
}
submitMessage() {
let newMessages = this.state.messages.concat([this.state.input]);
this.setState({input: "", messages: newMessages});
}
render() {
const renderedMessages = this.state.messages.map(msg => "<li>" + msg + "</li>");
//this might need to be converted to a string instead of an array ^
return (
<div>
<h2>Type in a new Message:</h2>
{ /* render an input, button, and ul here */ }
<input value={this.state.input} onChange={this.handleChange} />
<button onClick={this.submitMessage}>button</button>
<ul>
{renderedMessages}
</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/75.0.3770.100 Safari/537.36
.
Link to the challenge:
https://learn.freecodecamp.org/front-end-libraries/react-and-redux/manage-state-locally-first