If you want to run some code after a page (or component) is loaded but only want it to happen once during the entire lifetime of the application, you can consider using a combination of local storage or a global (context or state) mechanism that tracks whether the code has run before.
Here's a couple of approaches you can use:
1. Using Local Storage
If you want to ensure that the code runs only once across page visits, you can make use of localStorage. The idea is to check for a flag in local storage to determine if the code has already run.
import { useEffect } from 'react';
import { Container, Button } from '@mantine/core';
const MyComponent = () => {
useEffect(() => {
const hasRunBefore = localStorage.getItem('hasRunBefore');
if (!hasRunBefore) {
// Your code that you want to run only once
console.log('This code runs only once when the page loads.');
// Set the flag in local storage
localStorage.setItem('hasRunBefore', 'true');
}
}, []); // The empty array ensures this code runs only once on component mount
return (
<Container>
<h1>Welcome to My Page</h1>
<Button onClick={() => console.log('Button clicked!')}>Click me</Button>
</Container>
);
};
export default MyComponent;
2. Using React Context API
If you don't want to rely on local storage, you could use a context to track whether the action has already been executed. This allows you to control the execution at the application level.
Step 1: Create a Context
// Context.js
import { createContext, useContext, useState } from 'react';
const AppContext = createContext();
export const AppProvider = ({ children }) => {
const [hasRun, setHasRun] = useState(false);
return (
<AppContext.Provider value={{ hasRun, setHasRun }}>
{children}
</AppContext.Provider>
);
};
export const useAppContext = () => useContext(AppContext);
Step 2: Use Context in Your Component
// MyComponent.js
import { useEffect } from 'react';
import { useAppContext } from './Context';
import { Container, Button } from '@mantine/core';
const MyComponent = () => {
const { hasRun, setHasRun } = useAppContext();
useEffect(() => {
if (!hasRun) {
// Your code that you want to run only once
console.log('This code runs only once when the page loads.');
// Mark as run
setHasRun(true);
}
}, [hasRun, setHasRun]); // Include hasRun in the dependency array
return (
<Container>
<h1>Welcome to My Page</h1>
<Button onClick={() => console.log('Button clicked!')}>Click me</Button>
</Container>
);
};
export default MyComponent;
Step 3: Wrap Your Application with the Provider
Make sure you wrap your application (or at least the part where you want to use this context) with the AppProvider.
// _app.js
import { AppProvider } from '../path/to/Context';
function MyApp({ Component, pageProps }) {
return (
<AppProvider>
<Component {...pageProps} />
</AppProvider>
);
}
export default MyApp;
Conclusion
Both approaches allow you to run a function only once when the page loads. The local storage approach is simpler and works across page refreshes, while the context approach allows for more complex state management within your application's life cycle. Choose the one that best fits your application's architecture and requirements.