Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

add dark mode to react

// Dark Mode in React using Local Storage
import React from "react";
import { useState, useEffect } from "react";

const Home = () => {
  const [mode, setMode] = useState("light");
  const storedMode = localStorage.getItem("mode");
  if (storedMode == null) {
    localStorage.setItem("mode", "light");
  }
  useEffect(() => {
    setMode(storedMode);
  }, []);

  const handleClick = () => {
    if (storedMode == "light" || storedMode === null) {
      localStorage.removeItem("mode");
      localStorage.setItem("mode", "dark");
      setMode("dark");
    } else {
      localStorage.removeItem("mode");
      localStorage.setItem("mode", "light");
      setMode("Light");
    }
  };
  return (
    <div className="home">
      <h2>{mode}</h2>
      <button className={`button-${mode}`} onClick={handleClick}>
        Click Me
      </button>
    </div>
  );
};

export default Home;
Comment

react dark mode

import React from 'react'
import { func, string } from 'prop-types';
import styled from "styled-components"
const Button = styled.button`
  background: ${({ theme }) => theme.background};
  border: 2px solid ${({ theme }) => theme.toggleBorder};
  color: ${({ theme }) => theme.text};
  border-radius: 30px;
  cursor: pointer;
  font-size:0.8rem;
  padding: 0.6rem;
  }
`;
const Toggle = ({theme,  toggleTheme }) => {
    return (
        <Button onClick={toggleTheme} >
          Switch Theme
        </Button>
    );
};
Toggle.propTypes = {
    theme: string.isRequired,
    toggleTheme: func.isRequired,
}
export default Toggle;
Comment

react dark mode npm

$ npm i use-dark-mode
Comment

dark mode pure css react

/* define the property */
:root {
  --main-color: black;
}
/* use the property */
p {
  color: var(--main-color);
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: angular hostlistener 
Javascript :: javascript array.find 
Javascript :: get query params react 
Javascript :: using fetch api 
Javascript :: JS toString adds backslash 
Javascript :: console shortcut chrome 
Javascript :: exporting a class 
Javascript :: delete message discord.js 
Javascript :: make object to array javascript 
Javascript :: javascript slice array 
Javascript :: clear the command prompt node 
Javascript :: get match number array javascript 
Javascript :: javascript append array to array 
Javascript :: how to disable and enable a button in jquery 
Javascript :: react native new project mac 
Javascript :: check type of variable in javascript 
Javascript :: base64 encoded data to object in javascript 
Javascript :: onchange value in hidden input 
Javascript :: rotate camera three js 
Javascript :: initialize function in javascript 
Javascript :: jquery get img src 
Javascript :: js associative array push 
Javascript :: stringify json javascript 
Javascript :: how to get variable value outside function in javascript 
Javascript :: ng model on change 
Javascript :: array.from js 
Javascript :: link reload page react 
Javascript :: randomize an array in javascript 
Javascript :: javascript ternary operator 
Javascript :: how to choose a random name from a list in javascript 
ADD CONTENT
Topic
Content
Source link
Name
4+6 =