Git Product home page Git Product logo

exhaustive-deps-and-array-variables's Introduction

What is it?

This is a demo repository to visualize a problem of using ESlint together with react-hooks/exhaustive-deps when it is necessary to pass a variable of type Array to hook‘s dependency array

Context

React hooks have a way to pass dependencies as an array:

useEffect(() => {
  ...
}, [dependency1,dependency2]);

However, those dependencies are compared shallowly and that means if a dependency is of type Array React will not be able to detect changes within that array and it is necessary to calculate a derivative value of this array (e.g. array.join(',')). Similar suggestions were given by different authors: inventingwithmonster.io or facebook/react#14324 (comment)

The problem

react-hooks/exhaustive-deps is not able to detect that derived value is calculated for the dependency, an example of this can be seen here: src/Toasts.js#L27

image

also the suggested auto fix for this eslint problem actually breaks the code, as it changes

  useEffect(() => {
    setHeights(heights => calculateHeights(childKeys, refs, heights));
  }, [childKeys.join(',')]);

to

  useEffect(() => {
    setHeights(heights => calculateHeights(childKeys, refs, heights));
  }, [childKeys]);

which leads to an infinity loop

The question

I know that there is a workaround - simply disable the rule for a single line

// eslint-disable-next-line react-hooks/exhaustive-deps

However, it is barely desired outcome.

So I have a question of either:

a) maybe this situation is antipattern in the first place and there is an objective suggestion how to transform components that all dependencies would consist only of primitive types?

b) maybe it is worth to extend exhaustive-deps rule by adding an option, that the derived value of a dependency would be recognised as the dependency itself?

exhaustive-deps-and-array-variables's People

Contributors

lukaskl avatar

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.