Loading Thucde.dev
Preparing the next view.
1 min read
Sometimes, we want to run a snippet just once, even in development.

process.env.NODE_ENV === 'development'
We need an indicator to determine which environment are we running on.
const [counter, setCounter] = useState(0);
Add a counter state into the component
useEffect(() => { return () => setCounter((c) => c + 1); }, []);
Increase the counter in the clean-up function of useEffect
const shouldRunSomething = process.env.NODE_ENV === 'development' ? counter === 1 : true;
A variable to check if we should run the code
useEffect(() => { if (shouldRunSomething ) { // TO-DO } }, [shouldRunSomething]);
Use shouldRunSomething to only run the code in the second time.
const [counter, setCounter] = useState(0); const shouldRunSomething = process.env.NODE_ENV === 'development' ? counter === 1 : true; useEffect(() => { return () => setCounter((c) => c + 1); }, []); useEffect(() => { if (shouldRunSomething) { // TO-DO } }, [shouldRunSomething]);
The complete snippet