Coder Social home page Coder Social logo

Memoization about use-debounce HOT 4 CLOSED

xnimorz avatar xnimorz commented on August 28, 2024
Memoization

from use-debounce.

Comments (4)

JamesConsidine avatar JamesConsidine commented on August 28, 2024 1

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

from use-debounce.

JamesConsidine avatar JamesConsidine commented on August 28, 2024 1

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!

from use-debounce.

xnimorz avatar xnimorz commented on August 28, 2024

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 :)

from use-debounce.

xnimorz avatar xnimorz commented on August 28, 2024

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

from use-debounce.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.