Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memoization #59

Closed
JamesConsidine opened this issue Aug 14, 2020 · 4 comments
Closed

Memoization #59

JamesConsidine opened this issue Aug 14, 2020 · 4 comments

Comments

@JamesConsidine
Copy link

There is a heading in the documentation about memoization, but no example. I think you will need to allow a dependency array to be passed in, and make the usage the same as useCallback

@xnimorz
Copy link
Owner

xnimorz commented Aug 14, 2020

Hi @JamesConsidine, Thank you for the issue!
We don't use deps array explicitly.

Some refs describing why it's better:
https://twitter.com/mostruash/status/1109134981182488576 + #21

With Example: #53

To use memorization you could combine useCallback + useDebouncedCallback:

// memoize
const fetchPage = useCallback(activePage => {
    setContent({ title: activePage, body: "Lorem Ipsum" });
  }, []);

// debounce
  const [fetchPageDebounced] = useDebouncedCallback(fetchPage, 500);

In case you'd like to improve docs, I'll be glad to receive a PR :)

@JamesConsidine
Copy link
Author

Wow ok, I was clearly quite wrong here lol. Thanks for the example! I don't think I understand why we don't want to use deps though. Any chance you could give me a TLDR?

Re: the docs, I do think they need something to explain how to use it like that, so ill have a crack at a PR when I get a moment

@xnimorz
Copy link
Owner

xnimorz commented Aug 14, 2020

Any chance you could give me a TLDR?

The main reason is that automatic rules, like react-hooks/exhaustive-deps, don't check whether deps are right for custom hooks. They check only for the original hooks. It's why the react team don't recommend to make your own deps array for custom hooks:
https://twitter.com/dan_abramov/status/1109136157403680768
image

@JamesConsidine
Copy link
Author

Ok cool! I was seeing this as a replacement for useCallback, but really it supplements it. So yeah, I will close the issue and have a crack at the docs later. Thanks for all the help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants