You have some options:
- Use
onBlur
instead of onChange
so it will happen only when the user "leaves" the input
- Use
debounce
like this one.
But it depends on the experience and the ui impact of that change. I mean, if you need immediate impact (you present the value somewhere else in the UI), I don't think you have a choice.
If not, and you case is standart form
(or form
like), you can listen to onSubmit
and extract the values from the form
, something like this:
class App extends Component {
constructor() {
super();
this.state = {
name: 'React'
};
}
onSubmit = (e) => {
e.preventDefault();
const values = Array.from(e.target.elements).reduce((old, current) => {
if (current.attributes.name) {
old[current.name] = current.value
}
return old;
}, {})
this.setState(values);
}
render() {
return (
<>
<form onSubmit={this.onSubmit}>
<input name="name" placeholder="name" />
<input name="email" placeholder="email" />
<button>Submit</button>
</form>
<hr />
<pre>
{
JSON.stringify(this.state, null, 2)
}
</pre>
</>
);
}
}
https://stackblitz.com/edit/react-ewpsla