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

useCookie 在多处使用针对同一个 Key,refresh 没有进行广播 #91

Closed
StringKe opened this issue May 17, 2024 · 2 comments
Closed

Comments

@StringKe
Copy link

我现在是借助了 jotai 来进行状态同步,这个默认行为可能需要在文档提示一下?


const uiContainerAtom = atom('narrow');

export function useUiContainer() {
    const [cookieValue, updateCookie, refreshCookie] = useCookie('ui.is-container', {}, 'narrow');
    const [uiContainer, setUiContainer] = useAtom(uiContainerAtom);

    const onToggle = () => {
        updateCookie(cookieValue === 'full' ? 'narrow' : 'full');
        setUiContainer((prev) => (prev === 'full' ? 'narrow' : 'full'));
        refreshCookie();
    };

    useEffect(() => {
        setUiContainer(cookieValue || 'narrow');
    }, [cookieValue, setUiContainer]);

    const isFull = useMemo(() => uiContainer === 'full', [uiContainer]);

    return {
        isFull,
        onToggle,
    };
}

@childrentime
Copy link
Owner

很抱歉这么晚才回复,我一直在思考这个问题,预计后面会使用 useSyncExternalStore 来重构这个 hook, 看能否达到广播的效果。

@childrentime
Copy link
Owner

useCookie应该不会添加广播功能,因为这样相当于在内部维护了一个状态管理库,我觉得使用外部的状态管理库来同步是更好的选择,文档已经加了提示。#92

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

2 participants