Skip to content

Issue of "Maximum Update Depth Exceeded" in the ThemeProvider file's useEffect hook. #32

@Ritik-Yadav07

Description

@Ritik-Yadav07

Hi,
In the ThemeProvider.tsx, You are calling the handleChangeTheme() function inside the useEffect hook whenever there is change in the mode.
But in the handleChangeTheme method, we are setting the mode again by setMode(), this will change the mode and use effect will run again and this will goes in loop which will cause the issue of Maximum Update Depth Exceeded in the React.

 const handleChangeTheme = () =>{
    if(mode === "light") {
      setMode("dark");
      document.documentElement.classList.add("dark");
    } else {
      setMode("light");
      document.documentElement.classList.add("light");
      document.documentElement.classList.remove("dark");
    }
  }

  useEffect(() => {
    handleChangeTheme(); 
  }, [mode]);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions