Debounce onChange events
See original GitHub issueCurrently, for each keystroke in an input, a distinct action is dispatched. This results in a rather large action log that I’d like to avoid. Is there a way to effectively debounce the props.input.onChange
function? I’ve tried the following without success:
const DebouncedInput1 = props =>
<input
{...props.input}
onChange={debounce(props.input.onChange, 200)}/>
// -> debounced function is re-created on each render
class DebouncedInput2 extends Component {
constructor(props) {
super(props)
this.debounced = debounce(props.input.onChange, 200)
}
render() {
return (
<input
{...this.props.input}
onChange={this.debounced}/>
)
}
}
// -> react warns about reused synthetic events (and using `event.persist()` has no effect)
Thanks in advance for your feedback.
Issue Analytics
- State:
- Created 7 years ago
- Reactions:10
- Comments:6 (1 by maintainers)
Top Results From Across the Web
Debouncing events with React - Medium
The above code passes a debounced function as the event callback to the onChange event. The _.debounce function ensures that the actual onChange...
Read more >How to Correctly Debounce and Throttle Callbacks in React
Fortunately, debouncing and throttling techniques can help you control the invocation of the event handlers. In this post, you'll learn how ...
Read more >javascript - Debounce onChange - Stack Overflow
You are creating a debounced instance on every event call. Try creating one instance of debounced function and use it in your handler....
Read more >javaScript - debounce onchange input event - CodePen
Onchange Event used on user input fields. Only works on <input>, <select>, and <textarea>. Demo: Type into the inputs or click the button....
Read more >Implement Debouncing in React in 3 Different Ways
Debouncing is used for optimizing the performance of a web app. ... debouncing by implementing it on an input box with an onChange...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@smirea, thanks a lot for your detailed explanation. I didn’t manage to have the validation working correctly using only
defaultValue
(I’m still to new to this, I may have done a mistake somewhere) but your answer put me on the good track. Here’s what I ended up with:It’s a controlled component which manages the input value using both state and props. It might look cumbersome at first but it’s apparently what’s required to correctly handle:
when you do
<Field name='foo' component={DebouncedInput1} />
theinput
object you pass along contains avalue
prop that is passed directly from your redux state. That makes your input a controlled component which basically means its value only gets updated as a result of a state change, ergo you need to dispatch an action on every key press so the state gets update. Therefore debouncing doesn’t workIf you want to avoid triggering an action on every key (due to performance or smaller action log), you have to make it an uncontrolled field by not passing the
onChange
and thevalue
props. This will update your input only onblur
:This has a few effects since the state will now be updated on blur: