reactuse
reactuse copied to clipboard
useCookie 在多处使用针对同一个 Key,refresh 没有进行广播
我现在是借助了 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,
};
}