Skip to content
Advertisement

How to debounce a controlled input?

I’m currently struggling with react inputs and debounce from lodash. Most of the time when I have a form I also have an edit option, so I need a controlled component to fill back the inputs using value={state["targetValue"]} so I can fill and edit the field.

However, if the component is controlled debounce isn’t working. I made a simple example on CodeSandbox: https://codesandbox.io/embed/icy-cloud-ydzj2?fontsize=14&hidenavigation=1&theme=dark

Code:

JavaScript

Advertisement

Answer

So… Apparently, there’s no solution. the input takes the value from the state. While debounce prevents the state to trigger.

I made a workaround using ReactDOM.

JavaScript

and then the use:

JavaScript

This way I can fill my form with the state default value and continue using debounce.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement