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

Requires 2 clicks when reload on darkMode.value true #80

Open
yudyananda opened this issue May 7, 2021 · 3 comments
Open

Requires 2 clicks when reload on darkMode.value true #80

yudyananda opened this issue May 7, 2021 · 3 comments

Comments

@yudyananda
Copy link

yudyananda commented May 7, 2021

Hi, i'm using gatsby here. I found out that the toggle didn't work well with <input> onChange. It requires 2 clicks for state changes. This behaviour occurs when we reload the page with darkMode.value true

Here is the component

import * as React from 'react';
import useDarkMode from 'use-dark-mode';

const DarkmodeToggle = () => {
    
    const { toggle, value } = useDarkMode(false)

    return (
        <div>
            <label>
                <input
                    type="checkbox"
                    checked={value}
                    onChange={toggle}
                />  
            </label>
        </div>
    )
}

export default DarkmodeToggle

anyone here have the same issue?

@lukasoppermann
Copy link

I have a similar issue using a normal button to toggle though.

@yudyananda
Copy link
Author

@lukasoppermann interesting, in my case button works like a charm. My 2 cents is try to render the button in the client side.

if (typeof window === 'undefined') return null;

@markdost
Copy link

markdost commented Jun 24, 2021

Same here when only using a checkbox it does not change back to light mode and have to click twice.
With the buttons it works as expected.

        <input
          type="checkbox"
          className="custom-control-input"
          id="darkmodeSwitch"
          onChange={darkMode.toggle}
          checked={darkMode.value}
        />

hmm, I guess I solved it by adding onClick={darkMode.disable}

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

3 participants