onChange vs onKeyPress for input in React











up vote
1
down vote

favorite












I have an input form, and when you press enter I want it to call a function. I was wondering what "the right" way of doing this is.



Right now I have an input looking like this:



<input 
type="text"
placeholder="Enter key words"
value={this.state.value}
onChange={this.handleChange}
onKeyPress={this.handleEnter}
/>


And two functions that look like this



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

handleEnter(event) {
if (event.key === 'Enter') {
this.setState({value: event.target.value},function(){
this.props.theFunction(this.state.value);
});
}
}


Another solution was to point both onChange and onKeyPress to the same function and write it like



handleChangeAndEnter(event) {
if (event.key === 'Enter') {
this.setState({value: event.target.value},function(){
this.props.theFunction(this.state.value);
});
} else {
this.setState({value: event.target.value});
}
}


If I remove the onChange-binding the input-field stops updating itself on input (not entirely sure why this is) and if I remove the onKeyPress-binding the event-object no longer has a key-property.



My current solution works, but it still feels like a hack.










share|improve this question




























    up vote
    1
    down vote

    favorite












    I have an input form, and when you press enter I want it to call a function. I was wondering what "the right" way of doing this is.



    Right now I have an input looking like this:



    <input 
    type="text"
    placeholder="Enter key words"
    value={this.state.value}
    onChange={this.handleChange}
    onKeyPress={this.handleEnter}
    />


    And two functions that look like this



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

    handleEnter(event) {
    if (event.key === 'Enter') {
    this.setState({value: event.target.value},function(){
    this.props.theFunction(this.state.value);
    });
    }
    }


    Another solution was to point both onChange and onKeyPress to the same function and write it like



    handleChangeAndEnter(event) {
    if (event.key === 'Enter') {
    this.setState({value: event.target.value},function(){
    this.props.theFunction(this.state.value);
    });
    } else {
    this.setState({value: event.target.value});
    }
    }


    If I remove the onChange-binding the input-field stops updating itself on input (not entirely sure why this is) and if I remove the onKeyPress-binding the event-object no longer has a key-property.



    My current solution works, but it still feels like a hack.










    share|improve this question


























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I have an input form, and when you press enter I want it to call a function. I was wondering what "the right" way of doing this is.



      Right now I have an input looking like this:



      <input 
      type="text"
      placeholder="Enter key words"
      value={this.state.value}
      onChange={this.handleChange}
      onKeyPress={this.handleEnter}
      />


      And two functions that look like this



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

      handleEnter(event) {
      if (event.key === 'Enter') {
      this.setState({value: event.target.value},function(){
      this.props.theFunction(this.state.value);
      });
      }
      }


      Another solution was to point both onChange and onKeyPress to the same function and write it like



      handleChangeAndEnter(event) {
      if (event.key === 'Enter') {
      this.setState({value: event.target.value},function(){
      this.props.theFunction(this.state.value);
      });
      } else {
      this.setState({value: event.target.value});
      }
      }


      If I remove the onChange-binding the input-field stops updating itself on input (not entirely sure why this is) and if I remove the onKeyPress-binding the event-object no longer has a key-property.



      My current solution works, but it still feels like a hack.










      share|improve this question















      I have an input form, and when you press enter I want it to call a function. I was wondering what "the right" way of doing this is.



      Right now I have an input looking like this:



      <input 
      type="text"
      placeholder="Enter key words"
      value={this.state.value}
      onChange={this.handleChange}
      onKeyPress={this.handleEnter}
      />


      And two functions that look like this



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

      handleEnter(event) {
      if (event.key === 'Enter') {
      this.setState({value: event.target.value},function(){
      this.props.theFunction(this.state.value);
      });
      }
      }


      Another solution was to point both onChange and onKeyPress to the same function and write it like



      handleChangeAndEnter(event) {
      if (event.key === 'Enter') {
      this.setState({value: event.target.value},function(){
      this.props.theFunction(this.state.value);
      });
      } else {
      this.setState({value: event.target.value});
      }
      }


      If I remove the onChange-binding the input-field stops updating itself on input (not entirely sure why this is) and if I remove the onKeyPress-binding the event-object no longer has a key-property.



      My current solution works, but it still feels like a hack.







      comparative-review form event-handling react.js jsx






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 19 at 13:33









      200_success

      127k15148411




      127k15148411










      asked Mar 19 at 10:41









      MrJalapeno

      1063




      1063






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          I am new to React, so bear with me. ;)



          If you use the state in the enter handler exclusively, you don’t need the state at all.



          handleChangeAndEnter(event) {
          if (event.key !== 'Enter') { return; }
          this.props.theFunction(event.target.value);
          }


          If you use it for other things, I would recommend something like this:



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

          handleEnter(event) {
          if (event.key !== 'Enter') { return; }
          this.props.theFunction(this.state.value);
          }


          There is no need to change the state in handleEnter, because the state already reflects the current value.






          share|improve this answer





















            Your Answer





            StackExchange.ifUsing("editor", function () {
            return StackExchange.using("mathjaxEditing", function () {
            StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
            StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
            });
            });
            }, "mathjax-editing");

            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "196"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














             

            draft saved


            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f189920%2fonchange-vs-onkeypress-for-input-in-react%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote













            I am new to React, so bear with me. ;)



            If you use the state in the enter handler exclusively, you don’t need the state at all.



            handleChangeAndEnter(event) {
            if (event.key !== 'Enter') { return; }
            this.props.theFunction(event.target.value);
            }


            If you use it for other things, I would recommend something like this:



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

            handleEnter(event) {
            if (event.key !== 'Enter') { return; }
            this.props.theFunction(this.state.value);
            }


            There is no need to change the state in handleEnter, because the state already reflects the current value.






            share|improve this answer

























              up vote
              0
              down vote













              I am new to React, so bear with me. ;)



              If you use the state in the enter handler exclusively, you don’t need the state at all.



              handleChangeAndEnter(event) {
              if (event.key !== 'Enter') { return; }
              this.props.theFunction(event.target.value);
              }


              If you use it for other things, I would recommend something like this:



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

              handleEnter(event) {
              if (event.key !== 'Enter') { return; }
              this.props.theFunction(this.state.value);
              }


              There is no need to change the state in handleEnter, because the state already reflects the current value.






              share|improve this answer























                up vote
                0
                down vote










                up vote
                0
                down vote









                I am new to React, so bear with me. ;)



                If you use the state in the enter handler exclusively, you don’t need the state at all.



                handleChangeAndEnter(event) {
                if (event.key !== 'Enter') { return; }
                this.props.theFunction(event.target.value);
                }


                If you use it for other things, I would recommend something like this:



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

                handleEnter(event) {
                if (event.key !== 'Enter') { return; }
                this.props.theFunction(this.state.value);
                }


                There is no need to change the state in handleEnter, because the state already reflects the current value.






                share|improve this answer












                I am new to React, so bear with me. ;)



                If you use the state in the enter handler exclusively, you don’t need the state at all.



                handleChangeAndEnter(event) {
                if (event.key !== 'Enter') { return; }
                this.props.theFunction(event.target.value);
                }


                If you use it for other things, I would recommend something like this:



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

                handleEnter(event) {
                if (event.key !== 'Enter') { return; }
                this.props.theFunction(this.state.value);
                }


                There is no need to change the state in handleEnter, because the state already reflects the current value.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Mar 28 at 18:26









                Afterlame

                101




                101






























                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f189920%2fonchange-vs-onkeypress-for-input-in-react%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    List directoties down one level, excluding some named directories and files

                    list processes belonging to a network namespace

                    list systemd RuntimeDirectory mounts