Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

198
Vistas
How to throttle or debounce controlled input?

I am attempting to throttle this input field. The reason is because I want to save the input value into redux state and I read that you should not use redux state with onChange handlers because redux is not meant for high frequency state changes as opposed to local state. I tried the following but because it is a controlled input where the value is equal to the state the value is not updating.

 class Information extends Component {
    state = { 
myvalue:''
     }
     
changeHandler = (e) =>{

setTimeout(()=>{
    this.setState({
        myvalue:e.target.value
    })  

},1000)

}

    render() { 
        return (<div>
            <textarea value={this.state.myvalue} onChange={this.changeHandler} />


        </div>);
    }
}
 
export default Information;

 
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You probably don't want to delay setting the local state, because that will still cause the redux state update to happen at the same frequency. Your issue with the code provided is that the value isn't being updated because of setTimeout. You need to keep the input up-to-date in real time.

Instead, update the local state value in sync, and update the redux value with the setTimeout.

You'll need to set a variable for the timeout id, so you can remove it before setting another timeout. Once no changes have been made for the duration of the timeout, then the timeout will resolve, and the redux state can be updated.

constructor() {
  // set a property to name your timeout
  this.reduxTimer = null
}

changeHandler = (e) => {
  // clear an existing timeout
  clearTimeout(this.reduxTimer)

  // set your component state
  this.setState({ myvalue: e.target.value })

  // reset the timeout and assign its ID to the variable
  this.reduxTimer = setTimeout(() => {
    // set redux state
  }, 1000)
}
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda