use-dark-mode icon indicating copy to clipboard operation
use-dark-mode copied to clipboard

Requires 2 clicks when reload on darkMode.value true

Open yudyananda opened this issue 3 years ago • 3 comments

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?

yudyananda avatar May 07 '21 03:05 yudyananda

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

lukasoppermann avatar May 16 '21 07:05 lukasoppermann

@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;

yudyananda avatar May 16 '21 08:05 yudyananda

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}

markdost avatar Jun 24 '21 19:06 markdost