Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

175
Views
¿Por qué setInterval funciona en useEffect pero no cuando está dentro del contexto del componente funcional (vea el interior de la condición if)?

Estaba creando una interfaz de usuario para un diccionario que muestra una palabra seguida de su significado. La palabra cambia a través de un setInterval . En el siguiente código, estoy configurando setInterval inicialmente y luego en algunos eventos. Aquí solo estoy enfocado en el setInterval inicial.

Aunque tengo bastante experiencia en React, todavía no puedo entender por qué setInterval funciona cuando está dentro del gancho useEffect pero no desde la condición if.

Para replicar simplemente comente la condición if y elimine el comentario del bloque useEffect .

¿Puede alguien por favor explicar?

 import React, { useState, useEffect } from 'react'; import Dictionary from './Dictionary.json'; import './App.css'; const words = Object.keys(Dictionary); let currentInterval; function App() { const [randomWord, setRandomWord] = useState(words && words[Math.round(Math.random() * words.length)]); const [wordGenerationDuration, setWordGenerationDuration] = useState(10000); function selectRandomWord() { setRandomWord(words[Math.round(Math.random() * (words.length - 1))]); } if(!currentInterval) { currentInterval = setInterval(selectRandomWord, wordGenerationDuration); } // useEffect( // () => { // currentInterval = setInterval(selectRandomWord, wordGenerationDuration); // }, [] // ) return ( <div className='App'> <div className='dictionary-wrapper'> <p className='dictionary-word'> {randomWord} </p> <p className='dictionary-meaning'> {Dictionary[randomWord]} </p> <p className='next-button cursor-pointer'> <span onClick={ () => { clearInterval(currentInterval); selectRandomWord(); currentInterval = setInterval(selectRandomWord, wordGenerationDuration); } } > Next Word </span> </p> <p> <span>Auto Switch Duration:</span> <input className='dictionary-input' type='number' placeholder={wordGenerationDuration / 1000} onChange={ (evt) => { const wordGenerationDuration = Math.max(evt.target?.value * 1000, 10000); clearInterval(currentInterval); setWordGenerationDuration(wordGenerationDuration); currentInterval = setInterval(selectRandomWord, wordGenerationDuration); } } min='10' /> </p> </div> </div> ); } export default App;
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Su devolución de llamada de intervalo original es una función definida dentro de su componente ( selectRandomWord ).

Cuando su componente se vuelve a renderizar, el currentInterval se establecerá para que su intervalo no se redefina. El intervalo existente se referirá entonces a la versión de renderizado anterior de selectRandomWord que ya no actualiza el estado del renderizado actual.

Lo que realmente deberías hacer es

  1. Guarde el gancho de intervalo en una referencia
  2. Registre su intervalo dentro de useEffect() , y
  3. Asegúrese de borrar el intervalo en la limpieza de componentes
 function App() { const interval = useRef() const [randomWord, setRandomWord] = useState(words && words[Math.round(Math.random() * words.length)]); const [wordGenerationDuration, setWordGenerationDuration] = useState(10000); function selectRandomWord() { setRandomWord(words[Math.round(Math.random() * (words.length - 1))]); } useEffect(() => { if (!interval.current) { interval.current = setInterval(selectRandomWord, wordGenerationDuration) } // cleanup return () => { clearInterval(interval.current) } }, [])

También puede borrar el intervalo en cualquier momento usando

 clearInterval(interval.current)

Ver también https://reactjs.org/docs/hooks-faq.html#is-there-something-like-instance-variables

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!