skip to Main Content

Following is my DarkMode.jsx Component When I switch button ,dark mode apply only to navbar.
Other remaining Part of website not changed to Dark theme.How should I keep State updated everywhere.
using Material UI I am trying darkMode.

import React,{useState} from 'react';
import { ThemeProvider, createTheme } from "@mui/material/styles";
import CssBaseline from "@mui/material/CssBaseline";
import {  Switch,  } from "@mui/material"
export default function DarkMode() {

  // state to manage the dark mode
  const [toggleDarkMode, setToggleDarkMode] = useState(true);

  // function to toggle the dark mode as true or false
  const toggleDarkTheme = () => {
    setToggleDarkMode(!toggleDarkMode);
  };

  // applying the primary and secondary theme colors
  const darkTheme = createTheme({
    palette: {
      mode: toggleDarkMode ? 'dark' : 'light', // handle the dark mode state on toggle
      primary: {
        main: '#90caf9',
      },
      secondary: {
        main: '#131052',

      },
    },
  });

  return (
   <ThemeProvider theme={darkTheme}>
    <CssBaseline/>
    <div style={{ display: 'flex', flexDirection: 'column', alignItems: 'center' }}>
        <h2>Toggle Dark mode</h2>
        <Switch checked={toggleDarkMode} onChange={toggleDarkTheme} />
    </div>
   </ThemeProvider>
  )
}

I add my DarkMode Component inside Main.jsx after Navbar component

import React from 'react'
import ReactDOM from 'react-dom/client'
import './index.css'
// import App from './App'
import Navbar from './Components/components/Navbar';
import Hero from './Components/components/Hero';
import About from './Components/components/About';
import Feature from './Components/components/Feature';
import Card from './Components/components/Card';
import Opensource from './Components/components/Opensource';
import Footer from './Components/components/Footer';
import Workshop from './Components/components/Workshop';
import DarkMode from './Components/components/DarkMode';



ReactDOM.createRoot(document.getElementById('root')).render(
  <React.StrictMode>
    <Navbar/>
    <DarkMode/>
   <Hero />
   <Card />
   <About />
   <Feature />
   <Workshop/>
   <Opensource />
   <Footer />
  
  </React.StrictMode>,
)

2

Answers


  1. first in DarkMode Component you must return children props
    why? because in your structure you create theme only in the darkmode component and the other doesnt have accese to that

    import React,{useState} from 'react';
    import { ThemeProvider, createTheme } from "@mui/material/styles";
    import CssBaseline from "@mui/material/CssBaseline";
    import {  Switch,  } from "@mui/material"
    export default function DarkMode({children}) {
    
      // state to manage the dark mode
      const [toggleDarkMode, setToggleDarkMode] = useState(true);
    
      // function to toggle the dark mode as true or false
      const toggleDarkTheme = () => {
        setToggleDarkMode(!toggleDarkMode);
      };
    
      // applying the primary and secondary theme colors
      const darkTheme = createTheme({
        palette: {
          mode: toggleDarkMode ? 'dark' : 'light', // handle the dark mode state on toggle
          primary: {
            main: '#90caf9',
          },
          secondary: {
            main: '#131052',
          },
        },
      });
    
      return (
       <ThemeProvider theme={darkTheme}>
        <CssBaseline/>
        <div style={{ display: 'flex', flexDirection: 'column', alignItems: 'center' }}>
            <h2>Toggle Dark mode</h2>
            <Switch checked={toggleDarkMode} onChange={toggleDarkTheme} />
        </div>
       </ThemeProvider>
      )
    }
    

    and change the main.jsx to this:

    import React from 'react'
    import ReactDOM from 'react-dom/client'
    import './index.css'
    // import App from './App'
    import Navbar from './Components/components/Navbar';
    import Hero from './Components/components/Hero';
    import About from './Components/components/About';
    import Feature from './Components/components/Feature';
    import Card from './Components/components/Card';
    import Opensource from './Components/components/Opensource';
    import Footer from './Components/components/Footer';
    import Workshop from './Components/components/Workshop';
    import DarkMode from './Components/components/DarkMode';
    
    
    
    ReactDOM.createRoot(document.getElementById('root')).render(
      <React.StrictMode>
       <DarkMode> 
       <Navbar/>
         <Hero />
         <Card />
         <About />
         <Feature />
         <Workshop/>
         <Opensource />
         <Footer />
       </DarkMode>
      </React.StrictMode>,
    )
    
    Login or Signup to reply.
  2. To achieve a global dark mode that applies to the entire website, you need to manage the dark mode state at a higher level than just within the DarkMode component. One way to do this is by lifting the state up to a common parent component that wraps your entire application. This way, the dark mode state is accessible by all components that need to be aware of it.

    Create a state in the parent component (e.g., App.jsx) to manage the dark mode state.
    Pass down the state and the function to toggle dark mode as props to the DarkMode component.
    Update the DarkMode component to use the passed-down state and function.

    App.jsx

    import React, { useState } from 'react';
    import Navbar from './Components/components/Navbar';
    import Hero from './Components/components/Hero';
    import About from './Components/components/About';
    import Feature from './Components/components/Feature';
    import Card from './Components/components/Card';
    import Opensource from './Components/components/Opensource';
    import Footer from './Components/components/Footer';
    import Workshop from './Components/components/Workshop';
    import DarkMode from './Components/components/DarkMode';
    
    function App() {
      const [toggleDarkMode, setToggleDarkMode] = useState(true);
    
      const toggleDarkTheme = () => {
        setToggleDarkMode(!toggleDarkMode);
      };
    
      return (
        <div>
          <Navbar />
          <DarkMode toggleDarkMode={toggleDarkMode} toggleDarkTheme={toggleDarkTheme} />
          <Hero />
          <Card />
          <About />
          <Feature />
          <Workshop />
          <Opensource />
          <Footer />
        </div>
      );
    }
    
    export default App;
    

    DarkMode.jsx

    import React from 'react';
    import { ThemeProvider, createTheme } from '@mui/material/styles';
    import CssBaseline from '@mui/material/CssBaseline';
    import { Switch } from '@mui/material';
    
    export default function DarkMode({ toggleDarkMode, toggleDarkTheme }) {
      const darkTheme = createTheme({
        palette: {
          mode: toggleDarkMode ? 'dark' : 'light',
          primary: {
            main: '#90caf9',
          },
          secondary: {
            main: '#131052',
          },
        },
      });
    
      return (
        <ThemeProvider theme={darkTheme}>
          <CssBaseline />
          <div style={{ display: 'flex', flexDirection: 'column', alignItems: 'center' }}>
            <h2>Toggle Dark mode</h2>
            <Switch checked={toggleDarkMode} onChange={toggleDarkTheme} />
          </div>
        </ThemeProvider>
      );
    }
    
    

    Now, the dark mode state is managed in the App component, and both Navbar and DarkMode components have access to the same state. This way, when you toggle dark mode in the DarkMode component, it will affect the entire application.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search