【问题标题】:React - localstorage how to add multiple color themesReact - localstorage 如何添加多种颜色主题
【发布时间】:2021-01-24 03:15:08
【问题描述】:

我有一个复选框,可以将颜色主题更改为深色或浅色。但是我想再添加三个复选框,它们也将是页面的颜色,也就是说,我想要有 4 个复选框,其中第一个将页面颜色更改为黑色,第二个白色,第三个变为绿色,第四个变红

App.js

import React, { useState, useEffect } from "react";
import "./App.css";
import ReactDOM from 'react-dom';

export default function App() {
  const [dark, setDark] = useState(getMode);

  useEffect(() => {
    localStorage.setItem("dark", JSON.stringify(dark));
  }, [dark]);

  function getMode() {
    const savedmode = JSON.parse(localStorage.getItem("dark"));
    return savedmode || false;
  }
  
  return (
    <div className={dark ? "black" : "light"} style={{ height: "100vh" }}>
      <nav>
        <div className="toggle-container">
          <span style={{ color: dark ? "grey" : "yellow" }}>☀︎</span>
          <span className="toggle">
            <input
              checked={dark}
              onChange={() => setDark((prevMode) => !prevMode)}
              id="checkbox"
              className="checkbox"
              type="checkbox"
            />
            <label htmlFor="checkbox" />
          </span>
          <span style={{ color: dark ? "slateblue" : "grey" }}>☾</span>
        </div>
      </nav>

      <div style={{ textAlign: "center" }}>
        <h1>{dark ? "darkmode" : "lightmode"}</h1>
        <p>This is dark mode content</p>
        <h1>The darkmode is implemented by reactjs</h1>
      </div>
    </div>
  );
}

App.css

.black {
    background-color: black;
    color: white;
  }
  .light {
    background-color: blanchedalmond;
    color: black;
  }
  .nav {
    background-color: blue;
    color: white;
  }
  .toggle-container {
    background-color: blue;
    padding: 20px;
    display: flex;
    justify-content: center;
  }

【问题讨论】:

    标签: javascript reactjs function local-storage themes


    【解决方案1】:

    将活动颜色存储到 localStorage 例如

    localStorage.setItem('color', color);
    

    根据它设置颜色:

     const [color, setColor] = useState(localStorage.getItem("color"));
    
    
      const changeBg = (backgroundColor) => {
        localStorage.setItem('color', backgroundColor);
        setColor(backgroundColor)
      };
    

    并使用它来设置 backgroundColor 样式:

     <div style={{ backgroundColor: color }}>
          <div>
            <label>
              <input
                onChange={() => changeBg("green")}
                type="radio"
                name="background"
              />{" "}
              Green
            </label>
            <label>
              <input
                onChange={() => changeBg("red")}
                type="radio"
                name="background"
              />{" "}
              Red
            </label>
            <label>
              <input
                onChange={() => changeBg("blue")}
                type="radio"
                name="background"
              />{" "}
              Blue
            </label>
          </div>
        </div>
    

    另一种方法是设置 className 而不是样式,但您需要为每种颜色定义一个 css 规则,例如 .red: {...}, .black: {... } 等

    【讨论】:

    • 我无法设置颜色,如果不困难,您无法使用整个代码编辑您的问题
    • @Synchro 我更新了我的答案。请检查一下,如果它解决了您的问题,请告诉我。
    【解决方案2】:

    检查下面链接上的代码

    Code Sandbox

    我所做的只是添加

    const [bg, setBg] = useState("bg-white"); // this will hold classname for background;
    
    const changeBg = (color) => setBg(color);
    

    并将其添加到 App 类名中

    <div className={`App ${bg}`}>
    <label>
       <input checked={bg === "dark"} onChange={() => changeBg("bg-red")} /> Dark
    <label>
    </div>
    

    我认为代码是直截了当的,如果你有什么不明白的地方问我

    【讨论】:

    • 您的代码工作正常,只是暗模式复选框有问题,我可以轻松切换到暗模式以外的其他模式,例如,如果我切换到绿色,暗模式仍然存在,即,复选框没有从中删除,你能解决这个问题吗?
    • 现在检查一下,我只是删除暗状态并在 bg 中添加暗模式
    • 我检查了一切正常,打扰了,但是我之前切换到深色或浅色模式并刷新页面时发现了另一个小问题,颜色没有通过,但是现在当我切换时其中一种模式并刷新页面,提供颜色,我认为这是由于本地存储,但之前没有发生过
    • 我有点不专心,很遗憾你没有使用本地存储,这就是问题所在
    猜你喜欢
    • 1970-01-01
    • 2020-01-02
    • 2018-06-14
    • 2020-08-26
    • 2019-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-15
    相关资源
    最近更新 更多