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

176
Views
Nextjs: useEffect only once (if I use Link component)

I use Link component for open pages without reload:

<Link href="/home"><a>Home</a></Link>
<Link href="/page"><a>Page</a></Link>

This in my home:

const loadedRef = useRef(false)
useEffect(() => {
    if(!loadedRef.current){
        console.log("run")
        loadedRef.current = true;
    }
}, []);

This work fine for first load.

If I click on page and click on home, useEffect run again!

I want only and only once load useEffect even click another pages and return to home

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

useRef creates a value for this specific instance of the home component. If the instance unmounts, then that value goes away. If you want to make a global variable that persists even after the component has unmounted, do something like this:

import React, { useEffect } from 'react';

// Deliberately making this *outside* the component
let loaded = false;

const Home = () => {
  useEffect(() => {
    loaded = true;
  }, []);

  // ...
}

export default Home;
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!