简体   繁体   English

React - 输入复选框 onchange 不会第一次触发

[英]React - input checkbox onchange won't fire first time

The behaviour of a checkbox input is not being very reliable.复选框输入的行为不是很可靠。

I'm setting the state of a checkbox based on state from the store.我正在根据商店中的 state 设置复选框的 state。 This works fine.这工作正常。 However sometimes checking the checkbox will not fire the onchange .但是有时选中复选框不会触发onchange Checking the box a second time will fire, however the UI will display the wrong state as it misfired the first time.再次选中该框将触发,但 UI 将显示错误的 state,因为它第一次触发失败。

I'm using NextJS.我正在使用 NextJS。 This mostly happens when loading the page for the first time, or hard refresh.这主要发生在第一次加载页面或硬刷新时。

import { useDispatch, useSelector } from 'react-redux';
import _cloneDeep from 'lodash/cloneDeep';

export default function Privacy() {
    const consentObj = useSelector((state) => state.cookieOptions);
    const dispatch = useDispatch();

    const analyticsConsent = useSelector(
        (state) => state.cookieOptions.analytics
    );

    function toggleConsentAnalytics() {
        // this function will not fire on first input onchange
        const newSettings = _cloneDeep(consentObj);
        newSettings.analytics = !consentObj.analytics;

        dispatch({ type: 'SET_COOKIE_OPTIONS', payload: newSettings });
    }

    return (
        <div>
            <div className={css.settingContainer}>
                <strong>Analytics cookie</strong>
                <label>
                    <div className={css.switchWrap}>
                        {analyticsConsent ? 'On' : 'Off'}
                        <span className={css.switch}>
                            <input
                                defaultChecked={analyticsConsent}
                                onChange={() => toggleConsentAnalytics()}
                                type="checkbox"
                            />
                            <span className={css.slider}></span>
                        </span>
                    </div>
                </label>
            </div>
        </div>
    );
}

I've removed what I think is irrelevant code from my example.我已经从我的示例中删除了我认为不相关的代码。

I have an identical flow using a Class component and it works as expected.我使用 Class 组件有一个相同的流程,它按预期工作。 But it seems that we should be moving away from this into functional components so would very much like to solve this rather than revert to a Class!但似乎我们应该从这个转向功能组件,所以非常想解决这个问题而不是恢复到一个类!

Thanks in advance for any assistance提前感谢您的任何帮助

to make sure you have the right state of your checkbox, you can just use the checked attribute:为确保您的复选框具有正确的 state,您可以使用选中的属性:

<input type="checkbox" 
  checked={myValue} 
  onChange={(e) => setMyValue(e.target.checked)} 
/>

You can use onClick instead of onChange and it will work properly.您可以使用onClick代替onChange ,它将正常工作。

This has nothing to do with react, but with how the onchange event works in html.这与反应无关,而是与 onchange 事件在 html 中的工作方式有关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM