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

136
Views
¿Cuál es la forma correcta de usar prompt() dentro de useEffect en React.StrictMode para que no se ejecute dos veces?

React.StrictMode representa la página dos veces para fines de depuración. Tengo un aviso () en una página, que se ejecuta dos veces debido a esto, y he estado tratando de solucionarlo, ya que me gustaría mantener StrictMode.

Intenté esto, pero no funcionó:

 useEffect(() => { let identified = false; if (!identified) { let pName = prompt("Insert name"); setPlayerName(pName); } return () => { identified = true; } }, [])

También probé esto:

 const [playerName, setPlayerName] = useState(''); useEffect(() => { if (!playerName) { let pName = prompt("Insira name"); setPlayerName(pName); } }, [])

Y esto...:

 const [playerName, setPlayerName] = useState(''); useEffect(() => { if (!playerName) { let pName = prompt("Insira name"); return () => { setPlayerName(pName);; } } }, [])

pero no puedo hacerlo bien. ¿Cuál es la forma correcta de hacerlo?

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Aquí puedes encontrar información sobre cómo hacerlo:

Los efectos que solo deberían ejecutarse una vez pueden usar una ref.

 const didLogRef = useRef(false); useEffect(() => { // In this case, whether we are mounting or remounting, // we use a ref so that we only log an impression once. if (didLogRef.current === false) { didLogRef.current = true; SomeTrackingAPI.logImpression(); } }, []);

En tu escenario:

 const didPromptRef= useRef(false); useEffect(() => { if (didPromptRef.current === false) { didLogRef.current = true; const pName = prompt("Insert name"); setPlayerName(pName); } }, []);
about 4 years ago · Santiago Trujillo 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!