Do you mean to bind the submitMessage function, as you have the handleChange? Look at those two lines, there’s something missing in the submitMessage binding.
There is no display and understanding the button element in render seems to be the problem that needs help. It has been hard to search for explanations on the value attribute in an input element. I do not know where the value attribute comes from, maybe event.target.value. The value attribute is then used in the input element with the event handler. Do both value and the event handler need to be in an input element?
The example is the first app.
const handleSubmit = e => {
e.preventDefault();
alert("you have searched for - " + value);
// or you can send to backend
};
return (
<div className="App">
<form>
<input value={value} onChange={handleChange}/>
<button onClick={handleSubmit} type="submit">
Submit
</button>
</form>
</div>
);
}
The value questions are solved. Value is the input of an input element. An input element will take change from an event handler method and input from value.
This is the updated code that now does display something. The listing is being worked on.
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() {
this.setState({input: "", messages: this.state.messages.concat(this.state.input)
});
}
render() {
// let listItems = messsages.map(item =>
// <li>{item}</li>
// );
return (
<div>
<h2>Type in a new Message:</h2>
{ /* Render an input, button, and ul below this line */ }
<input type="text" onChange={this.handleChange} value={this.input} />
<button type="submit" onClick={this.submitMessage}>Add message</button>
<ul />
{ /* Change code above this line */ }
</div>
);
}
};