Git Product home page Git Product logo

Comments (5)

malte-wessel avatar malte-wessel commented on August 29, 2024

What react does is, if you don't pass inputs it takes the first argument of the hook as input:

// useCallback
const inputsNext = inputs !== undefined && inputs !== null ? inputs : [callback];
const dirty = shallowEqualsArray(inputsNext, inputsPrev);

from melody.

jamesb3ll avatar jamesb3ll commented on August 29, 2024

Hey Malte 😃I've created a codesandbox to see React's behavior (open the console):
https://codesandbox.io/s/52ylkpl26p

From what I can see:

  • useEffect(fn => cleanup(), inputs) will run fn on mount. Also, the cleanup function and fn will run on every update if inputs is an array of values that have changed (or is undefined).
  • useMemo(fn, inputs) will re-run fn every time the inputs changes (or is undefined).
  • useCallback(fn, inputs) is equal to useMemo(() => fn, inputs).

If any of the above are passed a constant array (eg. [] or ['constValue']) as inputs, then fn will only run once.

If on a re-render any of the above have inputs === undefined and fn is changed, the new fn will be run, so useEffect(fn) is actually useEffect(fn, [fn]).

I've not seen Melody's behaviour, but I presume it is different right now?

from melody.

pago avatar pago commented on August 29, 2024

I think it makes sense to align the behaviour. That'd make it more predictable.

from melody.

malte-wessel avatar malte-wessel commented on August 29, 2024

Hey @jamesb3ll 😍
yes, that's the behaviour that makes sense and that we also should implement. I will create a PR soon

from melody.

pago avatar pago commented on August 29, 2024

Has been fixed in #80

from melody.

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.