useRecoilRefresher signature doesn't play nice with selectorFamily
See original GitHub issueCurrently, 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:
- Created 2 years ago
- Comments:5 (3 by maintainers)
Top GitHub Comments
For example, with 0.6 you could do the following as a workaround for now:
Updated API feedback for #804 RFC
Ah, it hasn’t quite been released yet, but coming shortly. See the 0.6 documentation here.