use-between icon indicating copy to clipboard operation
use-between copied to clipboard

Preparing for release 1.4

Open betula opened this issue 3 years ago • 1 comments

The release will contain changes to the lifecycle of the shared states. According to:

  • https://github.com/betula/use-between/issues/28
  • https://github.com/betula/use-between/issues/17

Behaviour by default

The default lifecycle will be exactly what users obviously expect.

As long as the shared state has connections it is mounted, when owners are not, it is unmounted.

Infinitely live shred state

And for situations where you want to have an infinitely live shred state, there is a new way to call the useBetween hook

const useDarkModeState = () => {
  const [isDark, setDark] = useState(
    localStorage.getItem("darkMode")
  );

  useEffect(
    () => localStorage.setItem("darkMode", isDark ? "true" : ""), 
    [isDark]
  );

  const toggle = useCallback(
    () => setDark((state) => !state), 
    [setDark]
  );

  return {
    isDark,
    toggle
  };
};

If you want to have an infinitely live shred state you should use useBetween.forever instead of regular useBetween.

export const useDarkMode = () => useBetween.forever(useDarkModeState);

Resume

Going back to the default behavior where a shread state only exists when someone needs it is a serious performance optimization.

I believe that such behavior would be correct and logical. That this is exactly what users expect!

Have a Great Code!

betula avatar Jul 15 '22 12:07 betula

+1

melloware avatar Aug 07 '22 20:08 melloware