Giter Site home page Giter Site logo

gnowland / eslint-plugin-react-hooks-unreliable-deps Goto Github PK

View Code? Open in Web Editor NEW
3.0 1.0 0.0 185 KB

Extends ESLint rules for React Hooks to check for potential reference equality issues in dependency arrays

Home Page: https://npmjs.com/package/eslint-plugin-react-hooks-unreliable-deps

License: MIT License

JavaScript 100.00%
eslint eslint-react eslint-react-hooks optimization react react-hooks

eslint-plugin-react-hooks-unreliable-deps's Introduction

eslint-plugin-react-hooks-unreliable-deps

npm

This is a companion ESLint plugin for eslint-plugin-react-hooks to warn about potential issues arising from reference equality in React Hooks API dependency arrays.

Installation

Assuming you already have ESLint installed, run:

# npm
npm install eslint-plugin-react-hooks-unreliable-deps --save-dev

# yarn
yarn add eslint-plugin-react-hooks-unreliable-deps --dev

Then extend the recommended eslint config:

{
  "extends": [
    // ...
    "plugin:react-hooks-unreliable-deps/recommended"
  ]
}

Custom Configuration

If you want more fine-grained configuration, you can instead add a snippet like this to your ESLint configuration file:

{
  "plugins": [
    // ...
    "react-hooks-unreliable-deps"
  ],
  "rules": {
    // ...
    "react-hooks-unreliable-deps/reference-deps": ["warn", {
      "avoidObjects": true
    }]
  }
}

Advanced Configuration

reference-deps can be configured to validate dependencies of custom Hooks with the additionalHooks option. This option accepts a regex to match the names of custom Hooks that have dependencies.

{
  "rules": {
    // ...
    "react-hooks-unreliable-deps/reference-deps": ["warn", {
      "additionalHooks": "(useMyCustomHook|useMyOtherCustomHook)",
      "avoidObjects": true
    }]
  }
}

We suggest to use this option very sparingly, if at all. Generally saying, we recommend most custom Hooks to not use the dependencies argument, and instead provide a higher-level API that is more focused around a specific use case.

Explanation

Please refer to this wonderful treatise Object & Array Dependencies in the React useEffect Hook to learn more about the impetus behind this rule.

License

This project was forked from facebook/react/.../eslint-plugin-react-hooks and is inherently licensed under MIT.

Development

Clone the repository and run npm install to initialize. Run npm run build to create the build packages.

eslint-plugin-react-hooks-unreliable-deps's People

Contributors

gnowland avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

eslint-plugin-react-hooks-unreliable-deps's Issues

[bug] false trigger for `primitive` states that are passed from parent component

if I pass a primitive state from a parent component to child component and use that state in useEffect, your eslint rules triggers a false error: React Hook useEffect has an object in its dependency array: 'hover'. Non-primitive dependencies can result in triggering the callback unnecessarily due to referential equality comparison. Consider destructuring the object outside the useEffect call or using property accessors to refer to primitive values within the dependency array.eslint[react-hooks-unreliable-deps/reference-deps](https://www.benmvp.com/blog/object-array-dependencies-react-useEffect-hook/)

screenshot 2024-03-11 at 12 47 11@2x

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.