0

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;
Filburt
  • 17,626
  • 12
  • 64
  • 115
Alex T
  • 13
  • 4

2 Answers2

2

To achieve that behaviour use uncontrolled component, means don't use the value property of input element. Instead of using onChange event use onKeyDown and check the keyCode, update the state only when user press the enter key.

Check this working snippet:

class App extends React.Component {
  constructor(props) {
    super(props);

    this.state = { userInput: '' };
    this.handleUserInput = this.handleUserInput.bind(this);
  }

  handleUserInput(e) {
    if(e.keyCode == 13)
        this.setState({userInput: e.target.value});
  }

  render() {
    return (
       <div>    
          <input 
             type="text" 
             onKeyDown={this.handleUserInput}
          />
          <h1>{this.state.userInput}</h1>
       </div>      
     );
  }
}

ReactDOM.render(<App/>, document.getElementById('app'))
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>

<div id='app'//>
Mayank Shukla
  • 100,735
  • 18
  • 158
  • 142
1

@Mayank Shukla answer may be right, however it changes it to uncontrolled component.

Demo

You just need to add on more event onKeyPress which waits for "Enter" key.

class App extends React.Component {
  constructor(props) {
    super(props);

    this.state = { userInput: '', output: '' };
    this.handleUserInput = this.handleUserInput.bind(this);
    this.handleChange = this.handleChange.bind(this);
  }

  handleUserInput(e) {
    if (e.key === 'Enter') {
        this.setState({output: e.target.value});
    }
  }

  handleChange(e) {
    this.setState({userInput: e.target.value});
  }

  render() {
    return (
       <div>    
          <input 
           type="text" 
           value={this.state.userInput}
           onKeyPress={this.handleUserInput} // Only for "Enter" purpose
           onChange={this.handleChange}
           />
          <h1>{this.state.output}</h1>
       </div>      
     );
  }
}

This way you still keep the component controlled.

Example of Controlled Component objective

Leandro Soares
  • 2,902
  • 2
  • 27
  • 39
  • what is the difference between controlled and uncontrolled components? I apologize if it seems like a silly question, but I really am new, not even a week of studying. Also, thank you for your replies. – Alex T Jul 13 '17 at 11:01
  • 1
    @AlexT, You can see @Mayanka Shukla answer for that. Short resume: Uncontrolled means that you are not providing `value` to the input which means it gets controlled by the browser. While Controlled means that you are providing `value` and `onChange`. – Leandro Soares Jul 13 '17 at 11:03
  • @AlexT, also, controlled components input may be changed in other functions e.g if you want to change the value of the textbox if the users clicks on a button. – Leandro Soares Jul 13 '17 at 11:04
  • I see, thank you so much, I hope I manage to get my small project for myself done. – Alex T Jul 13 '17 at 11:05
  • @AlexT, I added an example on what you may do with a controlled component. – Leandro Soares Jul 13 '17 at 11:08
  • Another question, but please correct me if I'm wrong, this code you gave me, the way I read it, is that, everytime I type something on the input box, the state still changes per key press, we just don't see it, correct? – Alex T Jul 13 '17 at 11:09
  • @AlexT, Yes the render is called because of `onChange`. You can see the "keypress" -> render effect, the input of the textbox changes. That's the objective of controlled components. – Leandro Soares Jul 13 '17 at 11:10
  • yeah, I meant the changes on the

    tag, but I understand now. Thank you!

    – Alex T Jul 13 '17 at 11:13