score:1

Accepted answer

the error is because on initial render phase, you render the component with setisdashboard(true);, usually, you want to do it on mount (useeffect with empty dep array).

there is an initial render phase, then mount phase, see component's lifecycle diagram.

be sure that setisdashboard is persistent, meaning it is created by react api (like usestate).

or its memoized with usememo/usecallback, or you will get inifite loop because on every render a new instance of setisdashboard will be created and the dep array ([setisdashboard]) will cause another execution.

const protectedroute = ({ component }) => {
  const { setisdashboard } = usecontext(dashboardcontext);

  // make sure `setisdashboard` persistant
  useeffect(() => {
    setisdashboard(true);
  }, [setisdashboard]);

  ...
};

score:0

the error is because you can't set state when you are rendering:

dashboardcontext.setisdashboard(true); is probably the problem.

you don't post your stack trace or line numbers so it's hard to tell exactly what the issue is:

https://stackoverflow.com/help/minimal-reproducible-example


Related Query

More Query from same tag