Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Doesnt work correctly with NextJS #66

Open
dukesx opened this issue Dec 15, 2020 · 4 comments
Open

Doesnt work correctly with NextJS #66

dukesx opened this issue Dec 15, 2020 · 4 comments

Comments

@dukesx
Copy link

dukesx commented Dec 15, 2020

I am trying to use this with Next JS in _app.js but it seems that despite SSR support, there is some issue. Mechanism:

  • Fetch from System/Local Storage on first server render (SSR) in _App.js
  • "Value" argument from useDarkMode is passed to a theme switcher
  • in other components, i use "enable" and "disable" from useDarkMode to switch between light and dark mode
  • "Value" doesn't change. Need to refresh page two times or more in order to make it work

No Error in console. It just doesn't change useDarkMode "value" prop no matter how many times i enable() or disable().

  • Windows 10
  • useDarkMode latest version
  • Next JS 10
  • Node JS 14.15
@donavon
Copy link
Owner

donavon commented Dec 18, 2020

do you have a small app that I can try this out on?

@dukesx
Copy link
Author

dukesx commented Dec 18, 2020

do you have a small app that I can try this out on?

@donavon here you go
https://github.com/dukesx/css-theme-switcher-sample

@donavon
Copy link
Owner

donavon commented Dec 18, 2020

useDarkMode keeps state, so there's no reason to use your own setState. I changed your code and this works for me:

  const { value } = useDarkMode(false, {
    classNameDark: 'dark',
  });
  // const [dark, setDark] = useState(value);
  const dark = value;

Also, in your implementation, the value returned from useDarkMode was only used to set the initial value of useState, so it would never change.

@donavon
Copy link
Owner

donavon commented Dec 18, 2020

In nav.js, you can just to this:

const { toggle } = useDarkMode();

then

        {theme == 'dark' ? (
          <i class="ri-sun-line ri-xl" onClick={toggle}></i>
        ) : (
          <i class="ri-moon-line ri-xl" onClick={toggle}></i>
        )}

No lambda function needed for the onClick handler. Also, you don't need to call disable or enable when toggle will work in both cases, although there's nothing wrong with explicitly calling each.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants