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

Toogle doesn't change when the checked prop's state is changes by another components or button #13

Open
cs50victor opened this issue May 17, 2021 · 0 comments

Comments

@cs50victor
Copy link

here is the code I'm working with

import { useEffect, useState } from "react"
import { useTheme } from "next-themes"
import {DarkModeSwitch} from "react-toggle-dark-mode"
import tw from "twin.macro"

const ThemeChanger = ({button}) => {
const [mounted, setMounted] = useState(false)
const { theme, setTheme } = useTheme()
const { themes } = useTheme()
const [isDarkMode, setDarkMode] = useState(true)

// When mounted on client, now we can show the UI
useEffect(() => setMounted(true), [])

const capitalize = (s) => {
if (typeof s !== 'string') return ''
return s.charAt(0).toUpperCase() + s.slice(1)
}

const switchTheme = () => {
if (mounted) {
setTheme(theme === "light" ? "dark" : "light")
theme === "light" ? setDarkMode(true) : setDarkMode(false)
}
};

return (
button === true
? (

)
: (<>
Theme:
{themes.map((t) => ( {capitalize(t)} ))}
<button
tw="rounded-xl bg-indigo-800 px-7 py-3 mx-3"
onClick={() => setDarkMode(!isDarkMode)}
>
Changing DarkModeSwitch's checked prop state doesn't change how it looks

</>
)

)
}

export default ThemeChanger

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

1 participant