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.

Feature Request: useRecoilValueMemo and useRecoilValueLoadableMemo

See original GitHub issue

Prerequire code

const counterSelector = selector({
    key: 'my counter',
    get: async () => fetch('/some/api')
});

function Counter() {
    const value = useRecoilValue(counterSelector);
    const refreshCounter = useRecoilRefresher(counterSelector);

    const addCounter = useCallback(async () => {
        await fetch('/add/counter');
        refreshCounter();
    }, []);
    
    return (
        <div>
            <div onClick={addCounter}>{ value }</div>
        </div>
    )
}

Currently useRecoilValue and useRecoilValueLoadable will cause the page to be replaced by suspense when the selector is refreshed, especially when it is not rendered for the first time, this behavior often interrupts the user’s operation, so I propose to add two hooks: useRecoilValueMemo and useRecoilLoadableMemo.

We can easily implement a memoize version

export function useRecoilValueMemo(recoilValue) {
    const loadable = useRecoilValueLoadable(recoilValue);
    const ref = useRef(null);

    if (loadable.state === 'hasValue') {
        ref.current = loadable.contents;
    }

    if (ref.current == null && loadable.state === 'loading') throw loadable.contents;

    if (loadable.state === 'hasError') {
        throw loadable.contents;
    }

    return ref.current;
}

Same idea for useRecoilValueLoadable.

export function useRecoilValueLoadableMemo(recoilValue) {
    const loadable = useRecoilValueLoadable(recoilValue);
    const ref = useRef(loadable);

    if (loadable.state === 'hasValue') {
        ref.current = loadable;
    }

    if (loadable.state === 'hasError') return loadable;

    return ref.current;
}

This can be more smooth for page refresh.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
Qquanweicommented, Aug 15, 2022

We should add new package like recoil-enhance , that will provide some great practice hooks. should we create another new package?

0reactions
Qquanweicommented, Sep 27, 2022

@signorbusi Thanks reply !

recoil-enhance was published , see also https://github.com/Qquanwei/recoil-enhance

Read more comments on GitHub >

github_iconTop Results From Across the Web

useRecoilValueLoadable(state) | Recoil
This hook is intended to be used for reading the value of asynchronous selectors. This hook will subscribe the component to the given...
Read more >
Use React Memo to Optimize Performance, Save - Copycat
In this article, you'll learn about the three APIs in a functional React Component: React memo(), useMemo, and useCallback for Memoization.
Read more >
Exploring Asynchronous Requests in Recoil - AppSignal Blog
There is a workaround via useRecoilValueLoadable , but this needs more ... Recoil has a useRecoilValue hook that fires the initial request ......
Read more >
Mastering React Memo - YouTube
React. memo, useMemo, useCallback, should you use them? When should you use them? Lets improve your React coding skills right now!
Read more >
How to use memo in React to Optimize Renders - YouTube
Learn how to optimize the number of times your component renders in React with the memo function.
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