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

445
Views
react-hooks: omitir la primera ejecución en useEffect

Cómo puedo omitir la primera ejecución en useEffect hook.

 useEffect(() => { const first = // ??? if (first) { // skip } else { // run main code } }, [id]);
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

El useRef se puede usar para almacenar cualquier valor mutable , por lo que podría almacenar un valor booleano que indique si es la primera vez que se ejecuta el efecto.

Ejemplo

 const { useState, useRef, useEffect } = React; function MyComponent() { const [count, setCount] = useState(0); const isFirstRun = useRef(true); useEffect (() => { if (isFirstRun.current) { isFirstRun.current = false; return; } console.log("Effect was run"); }); return ( <div> <p>Clicked {count} times</p> <button onClick={() => { setCount(count + 1); }} > Click Me </button> </div> ); } ReactDOM.render( <MyComponent/>, document.getElementById("app") );
 <script src="https://unpkg.com/react@16.7.0-alpha.0/umd/react.development.js"></script> <script src="https://unpkg.com/react-dom@16.7.0-alpha.0/umd/react-dom.development.js"></script> <div id="app"></div>

over 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!