Reactjs disruption in "getStorageTheme()"

65 Views Asked by At

In my project, there is a shape image. When I click the dark mode button, it turns the color I want. When I click the light mode button, it also turns the color I want. However, when I refresh the page, the color of the shape in dark mode remains the same as in light mode. How can I fix this?

Error! When I refresh the page shape color is Light-theme's color

Light-theme's shape color

Dark theme's shape color

Header.jsx (Full codes)

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 (Only useful codes for my question)

.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 (Only useful codes for my question)

/*--------------------------------------------------*\
#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;
}

What Did I Tried:

I tried making changes to the CSS color codes. I attempted to change the main theme in the function. I also tried more than one solution that came to my mind, but I couldn't succeed in any of them.

0

There are 0 best solutions below