-
-
Notifications
You must be signed in to change notification settings - Fork 46
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
Toggle - React/NextJS 13 (with hot fix) #30
Comments
man you save my life right here. Thank you for the hot fix |
@Biratus dude you deserve 🍻 |
Are there any plans for fixing this? It's been an issue for a long time. I feel like the provided solution renders the library itself somewhat pointless. |
Anyways, as inferred by OP, the cause is React's strictmode mounting all components twice. The following solution worked for me. useEffect(() => {
themeChange(false);
return () => {
themeChange(false);
};
}, []); This should work with both strictmode enabled as well as in production. |
Thanks 👍 |
I don't think this relates to Next.js at all (I have never used it). Strictmode is a React artifact, and yes, it is enabled by default in the newer versions (the general consensus seem to be that disabling strictmode is not recommended). The problem with most solutions I tried was that they'd work with strictmode on, and thus break in production, or work with strictmode off, and thus break in development. The above, however, seems to work in both scenarios. |
This should be included on the README for NextJS |
import { useEffect } from 'react'; export default function MyApp() { return ( // button
// daisy ui config plugins: [require('daisyui')], dont forget to use 'use client' on first line |
@dorukgezici @rcapdepaula thanks for the snippets; it works. My system preference is dark bg, so when I choose the light mode and refresh the app, I see dark bg for a second while it sets the light mode. How to get this working? instead of the button
|
Here is my full component; there might be a better way to do this.
|
Hello,
I just started using DaisyUI and tailwindcss. I want to say first and foremost that I am not an expert (with these libraries).
None of the examples or answers worked for me so I checked the code to see what is going on. And I want to share my findings and my solution.
The
themeChange(false)
did nothing so I investigated and put thethemeToggle
function in my useEffect and especially the last part.I added a return function to the useEffect to unsubscribe, with React 18 the listener is called twice so the toggle is useless (i.e. changes theme and reverts back to the initial one => No visual change on the page).
And that is all actually.
Here is my toggle component (I am using react-feather for icons):
tailwind.config.js
if needed:This is a hot fix for the problem I had. As I said I am in no way an expert with these libraries, just wanted to share so you guys could try and implement this in a nice way :) The BIG problem is regarding useEffect and React 18 that calls it twice I think.
Keep me updated if I have done blasphemous stuff with this code... I might take a look in the futur but it works for now..
Cheers !
The text was updated successfully, but these errors were encountered: