question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

useRecoilRefresher signature doesn't play nice with selectorFamily

See original GitHub issue

Currently, the signature is:

type Refresher = () => void;

function useRecoilRefresher_UNSTABLE(state: RecoilValue): Refresher

It would be nicer to have it like:

type Refresher = (state: RecoilValue) => void;

function useRecoilRefresher_UNSTABLE(): Refresher

Consider this example:

const fileContent = selectorFamily({
  key: "fileContent",
  get: (path: string) => async () => fetchFileContent(path),
});

Imagine I’m showing the list of files, in a FileList component, which renders FileItem components. If I want to have a hook for reloading file content from disk/network, etc., I can only implement it in a way that it gets the file path as the argument and returns a non-parametric reload function:

const reload = useReloadFileContent(filePath);
// reload();

Which means I can only use it in FileItem component. It would be nicer, if I were able to implement it like this:

const reload = useReloadFileContent();
// reload(filePath);

That would allow for having the reload action in either FileItem component, or in FileList component.

This is the current implementation: https://github.com/facebookexperimental/Recoil/blob/a58ce48c5e5139b59ae3e138566928ec5a431825/packages/recoil/hooks/Recoil_useRecoilRefresher.js#L19-L25

It seems quite feasible to change it to:

function useRecoilRefresher(): <T>(recoilValue: RecoilValue<T>) => void {
  const storeRef = useStoreRef();
  return useCallback(<T>(recoilValue: RecoilValue<T>) => {
    const store = storeRef.current;
    refreshRecoilValue(store, recoilValue);
  }, [storeRef]);
}

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
drarmstrcommented, Jan 9, 2022

For example, with 0.6 you could do the following as a workaround for now:

const refresh = useRecoilCallback(({refresh}) => node => refresh(node));

Updated API feedback for #804 RFC

1reaction
drarmstrcommented, Jan 5, 2022

Ah, it hasn’t quite been released yet, but coming shortly. See the 0.6 documentation here.

Read more comments on GitHub >

github_iconTop Results From Across the Web

selectorFamily(options) - Recoil
Returns a function that returns a read-only RecoilValueReadOnly or writeable RecoilState selector. A selectorFamily is a powerful pattern that is similar to a ......
Read more >
Use selectorFamily to take arguments in your Recoil selectors
In this lesson, we're going to learn how to create Recoil selectors that accept arguments. These are made possible using the selectorFamily ......
Read more >
Refresh recoil atom with async fetch, after it has been updated ...
Using atomFamily and selectorFamily let me do what I want, as I can specify an id. const answerState = atomFamily<AnswerDto[] | undefined, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found