“getStorageTheme()”中的 Reactjs 中断

Reactjs disruption in "getStorageTheme()"

提问人:Berkant Karakayış 提问时间:11/6/2023 最后编辑:Berkant Karakayış 更新时间:11/6/2023 访问量:41

问:

在我的项目中,有一个形状图像。当我单击深色模式按钮时,它会变成我想要的颜色。当我单击灯光模式按钮时,它也会变成我想要的颜色。但是,当我刷新页面时,深色模式下形状的颜色与浅色模式下的颜色相同。我该如何解决这个问题?

错误!当我刷新页面形状颜色时,颜色是浅色主题的颜色

浅色主题的形状颜色

深色主题的形状颜色

Header.jsx(完整代码)

import React, { useEffect, useState } from 'react'
import { links } from "../../Data";
import { FaTwitter, FaGithub, FaLinkedinIn, FaInstagram,  } from 'react-icons/fa';
import {BsSun, BsMoon} from 'react-icons/bs';
import './header.css';
import { Link } from 'react-scroll';
import { animateScroll } from 'react-scroll';

const getStorageTheme = () =>{
    let theme = 'light-theme';
    if(localStorage.getItem('theme')){
        theme = localStorage.getItem('theme');
    }
}

const Header = () => {
  const [showMenu, setShowMenu] = useState(false);
  const [scrollNav, setScrollNav] = useState(false);
  const [theme, setTheme] = useState(getStorageTheme());

  const scrollTop = () => {
    animateScroll.scrollToTop();
  };

  const changeNav = () => {
    if(window.scrollY >= 80) {
        setScrollNav(true);
    }
    else{
        setScrollNav(false);
    }
  };

  const toggleTheme = () => {
    if(theme === 'light-theme'){
        setTheme('dark-theme');
    }
    else{
        setTheme('light-theme');
    }
  };

  useEffect(() => {
    window.addEventListener('scroll', changeNav);
  }, []);

  useEffect(() => {
    document.body.classList.toggle('no-scroll', showMenu);
  }, [showMenu]);

  useEffect(() => {
    document.documentElement.className = theme;
    localStorage.setItem('theme', theme);
  }, [theme]);

  return (
    <header className={`${scrollNav ? 'scroll-header': ''}
    header`}>
        <nav className="nav">
            <Link to='/' onClick={scrollTop} className="nav__logo text-cs">
                Berkant
            </Link>

            <div className={`${showMenu ? 'nav__menu show-menu' :
            'nav__menu'}`}>
               <div className="nav__data">
                    <ul className="nav__list">
                        {links.map(({name, path}, index)=>{
                            return(
                                <li className="nav__item" key={index}>
                                    <Link 
                                        className='nav__link text-cs'
                                        to={path}
                                        spy={true} 
                                        hashSpy={true}
                                        smooth={true} 
                                        offset={-100} 
                                        duration={500} 
                                        onClick={() => setShowMenu(!showMenu)}
                                    >
                                        {name}
                                    </Link>
                                </li>
                            )
                        })}
                    </ul>

                    <div className='header__socials'>
                        <a href='https://twitter.com/berkantkrkyss' className='header__social-link'>
                            <FaTwitter />
                        </a>

                        <a href='https://www.linkedin.com/in/berkant-karakayis/' className='header__social-link'>
                            <FaLinkedinIn />
                        </a>

                        <a href='https://github.com/berkantkarakayis' className='header__social-link'>
                            <FaGithub />
                        </a>

                        <a href='https://www.instagram.com/berkantkrkys/' className='header__social-link'>
                            <FaInstagram />
                        </a>
                    </div>
               </div>
            </div>

            <div className="nav__btns">
                <div className="theme__toggler" onClick={toggleTheme}>
                    {theme === 'light-theme' ? <BsMoon /> : <BsSun />}
                </div>

                <div className={`${showMenu ? 'nav__toggle animate-toggle' :
                    'nav__toggle'}`} onClick={() =>
                    setShowMenu(!showMenu)}>
                    <span></span>
                    <span></span>
                </div>
            </div>
        </nav>
    </header>
  )
}

export default Header

header.css(仅对我的问题有用的代码)

.header{
    position: absolute;
    top: 0;
    width: 100%;
    padding: 30px;
    z-index: 100;
}

.scroll-header{
    position: fixed;
    background-color: var(--bg-color-alt);
    animation: header_animate 0.8s var(--transition) 0s forwards;
}

@keyframes header_animate{
    0% {
        transform: translateY(-100px);
    }

    100%{
        transform: translateY(0);
    }
}

.theme__toggler{
    font-size: var(--h4-font-size);
    display: flex;
    align-items: center;
}

.nav__toggle{
    height: 30px;
    width: 28px;
    position: relative;
    z-index: 100;
}

.theme__toggler,
.nav__toggle{
    cursor: pointer;
}

.nav__toggle span{
    position: absolute;
    left: 0;
    width: 100%;
    height: 2px;
    background-color: var(--title-color);
    transition: all 0.3s var(--transition);
}

.nav__toggle span:first-child{
    top: 8px;
}

.nav__toggle span:last-child{
    bottom: 8px;
}

.animate-toggle span:first-child{
    transform: rotate(-45deg);
    top: 14px;
}

.animate-toggle span:last-child{
    transform: rotate(45deg);
    bottom: 14px;
}


App.css(仅对我的问题有用的代码)

/*--------------------------------------------------*\
#CSS VARIABLES
\*--------------------------------------------------*/

:root {
    /**
    * colors
    */

    --primary-color: hsl(165, 60%, 40%);
    --title-color: hsl(0, 0%, 100%);
    --text-color: hsl(0, 0%, 92%);
    --bg-color: hsl(216, 18%, 16%);
    --bg-color-alt: hsl(213, 10%, 21%);
    --container-color: hsl(217, 18%, 14%);
    --border-color: hsl(210, 2%, 65%);
    --first-gradient: linear-gradient(
        0deg,
        var(--bg-color-alt) 0%,
        var(--bg-color) 100%
    );
    --second-gradient: linear-gradient(
        180deg,
        var(--bg-color-alt) 0%,
        var(--bg-color) 100%
    );
    --third-gradient: linear-gradient(
        180deg,
        var(--bg-color) 0%,
        var(--bg-color) 100%
    );


    /**
    * shadow
    */

    --shadow: 5px 5px rgb(255 255 255  / 10%);

}

/*--------------------------------------------------*\
#LIGHT THEME
\*--------------------------------------------------*/
.light-theme{
    --title-color: #000;
    --text-color: #262626;
    --bg-color: #f0ebe3;
    --bg-color-alt: #f0ebe3;
    --container-color: #fff;
    --border-color: #000;

      /**
    * shadow
    */

    --shadow: 5px 5px rgb(0 0 0  / 20%);
}

.light-theme .skills__bar{
    background: rgba(0, 0, 0, 0.1);
}

.light-theme .skills__percentage{
    background: var(--border-color);
}

.light-theme .skills__percentage span{
    background-color: var(--primary-color);
}


/*--------------------------------------------------*\
#REUSABLE CSS CLASSES
\*--------------------------------------------------*/

.dark-theme .shape{
    filter: invert(1);
    opacity: 0.6;
}

.shape{
    position: absolute;
}

.c__shape{
    right: -24px;
    bottom: -24px;
    width: 141px;
    height: 141px;
}

我试过什么:

我尝试更改CSS颜色代码。我试图改变函数中的主题。我还尝试了不止一种我想到的解决方案,但我都无法成功。

JavaScript CSS ReactJS 常量 组合

评论


答: 暂无答案