I'm a real beginner with ReactJs Es6, I'm trying to setState to my component when I hit the "Enter" Button, I have tried some of the answers here, like this one, https://stackoverflow.com/a/34634290/8301413, but it hasn't worked the way I want to.
What I have so far is when I enter a text on my input box, the <h1>
changes it's state and displays the state with every character input. What I want to happen is, I input my text first, then when I hit enter, that's the only time the state updates and displays the text from the input box.
import React, { Component } from 'react';
import ReactDOM from 'react-dom';
export class App extends Component {
constructor(props) {
super(props);
this.state = { userInput: '' };
this.handleUserInput = this.handleUserInput.bind(this);
}
handleUserInput(e) {
this.setState({userInput: e.target.value});
}
render() {
return (
<div>
<input
type="text"
value={this.state.userInput}
onChange={this.handleUserInput}
/>
<h1>{this.state.userInput}</h1>
</div>
);
}
}
export default App;
tag, but I understand now. Thank you!
– Alex T Jul 13 '17 at 11:13