I've got a page which needs to kick a user back to the previous one if they don't have permissions to view. I made this very simple component as a test but I'm finding that it navigates back two pages not one. Any ideas why?
import React, { useEffect, useState } from 'react';
import { useNavigate } from 'react-router-dom';
const GoBack = () => {
const navigate = useNavigate();
const [state, setState] = useState('init');
if (state === 'init') setState('goBack');
useEffect(() => {
if (state === 'goBack') navigate(-1);
}, [state]);
return <p>You are not permitted to view this page</p>;
};
export default GoBack;
You shouldn't call setState in the middle of the component's rendering process. This will cause the component to become impure. Strict mode helps you catch this bug early by causing the component to render twice, highlighting the inconsistency. What happened was:
You can use this to run with and without StrictMode to see the detail:
Move the if check into the
useEffectand the problem is gone. If your intend was to prevent users from seeing anything on the screen then you can use useLayoutEffect