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.
comparative-review form event-handling react.js jsx
add a comment |
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.
comparative-review form event-handling react.js jsx
add a comment |
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.
comparative-review form event-handling react.js jsx
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
comparative-review form event-handling react.js jsx
edited Mar 19 at 13:33
200_success
127k15148412
127k15148412
asked Mar 19 at 10:41
MrJalapeno
1063
1063
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Mar 28 at 18:26
Afterlame
101
101
add a comment |
add a comment |
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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