omerman / jotai-nexus

MIT License
11 stars 2 forks source link

Missing useCallback #1

Closed A-Shleifman closed 2 years ago

A-Shleifman commented 2 years ago

Thank you @omerman for publishing this!

This library didn't work for me in some scenarios and I noticed that it didn't have a useCallback wrapper inside the useAtomCallback function. According to the docs it's required. Adding it fixed the problem for me.

I also might be missing something, but why not just expose the functions provided by the hook itself? Something like this:

// JotaiNexus.tsx

import { Getter, Setter } from "jotai";
import { useAtomCallback } from "jotai/utils";
import { useCallback, useEffect } from "react";

export let readAtom: Getter;
export let writeAtom: Setter;

const JotaiNexus = () => {
  const init = useAtomCallback(
    useCallback((get, set) => {
      readAtom = get;
      writeAtom = set;
    }, [])
  );

  useEffect(() => {
    init();
  }, []);

  return null;
};

export default JotaiNexus;
omerman commented 2 years ago

Wow, to tell you the truth I'm not sure why I wrote it the way I did. The implementation you supplied is much more clean. Regarding the useAtomCallback point, Maybe I've seen an example in a different place without the useCallback.

I'll push a fix in the coming minutes, Thank you :)

omerman commented 2 years ago

@A-Shleifman Thanks for the issue, I added the fix you suggested. (I think I havent encountered a problem since my Root component never re-rendered).

Feel free to open issues if you find more ;)

A-Shleifman commented 2 years ago

Thank you very much! :)