reactuse icon indicating copy to clipboard operation
reactuse copied to clipboard

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

Open StringKe opened this issue 1 year ago • 0 comments

我现在是借助了 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,
    };
}

StringKe avatar May 17 '24 10:05 StringKe