Socket
Socket
Sign inDemoInstall

eslint-plugin-react

Package Overview
Dependencies
200
Maintainers
2
Versions
203
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    eslint-plugin-react

React specific linting rules for ESLint


Version published
Weekly downloads
15M
decreased by-17.43%
Maintainers
2
Install size
1.77 MB
Created
Weekly downloads
 

Package description

What is eslint-plugin-react?

The eslint-plugin-react is a plugin for ESLint that provides linting utilities for React and JSX specific code. It helps developers adhere to best practices and avoid common pitfalls in React development.

What are eslint-plugin-react's main functionalities?

JSX-specific rules

Enforces rules specific to JSX, such as ensuring that any variables used in JSX are defined and that React is in scope when using JSX.

"react/jsx-uses-react": "error", "react/jsx-uses-vars": "error"

Hooks rules

Provides rules that enforce the correct usage of React Hooks, such as ensuring that hooks are called in the same order on every render and that dependencies are specified correctly for useEffect.

"react-hooks/rules-of-hooks": "error", "react-hooks/exhaustive-deps": "warn"

Prop-types validation

Ensures that prop types are correctly defined and used within React components, helping to catch type-related bugs.

"react/prop-types": "error"

Component lifecycle

Warns about usage of deprecated methods in React component lifecycle, encouraging developers to update to newer APIs.

"react/no-deprecated": "warn"

Best practices

Encourages best practices by warning against common anti-patterns like using array indices as keys or mutating state directly.

"react/no-array-index-key": "warn", "react/no-direct-mutation-state": "error"

Other packages similar to eslint-plugin-react

Changelog

Source

[7.14.0] - 2019-06-23

Added

  • Add [jsx-curly-newline][] rule ([#1493][] @golopot)
  • Add support for nested destructuring to [prop-types][] ([#296][] [#1422][] @golopot)
  • Add support for variables defined as props to [prop-types][] and [no-unused-prop-types][] ([#442][] [#833][] [#1002][] [#1116][] [#1257][] [#1764][] @golopot)
  • Add checkFragmentShorthand option to [jsx-key][] ([#2316][] @kaykayehnn)

Fixed

  • Fix [no-did-mount-set-state][] and [no-did-update-set-state][] to handle cDU and cDM defined as class properties ([#1595][] @jaaberg)
  • Fix [sort-prop-types][] cash when a shape PropType is defined in a variable ([#1749][] @alexzherdev)
  • Fix [no-unused-state][] false positive when using state of non-lifecycle method ([#2274][] @golopot)
  • Fix [static-property-placement][] false positive when accessing static property inside method ([#2283][] @dmason30)
  • Fix [prop-type][] detection for annotated props with default value ([#2298][] @yannickcr)

Changed

  • Add ESLint 6.0.0 as valid peerDependency (@yannickcr)
  • Improve [no-render-return-value][] performance ([#2259][] @golopot)
  • Change [jsx-sort-props][] to report errors only on the identifier ([#2312][] @MrHen)
  • Change to warn only once if react version cannot be detected ([#2276][] @ljharb)
  • Documentation improvements ([#2263][] @dimitropoulos, [#2262][] @ybiquitous, [#2295][] @battaglr, [#2302][] @Jason-Cooke, [#2303][] @golopot)
  • Code refactoring ([#2265][] [#2267][] [#2286][] [#2294][] @golopot, @ljharb)
  • Tests improvements ([#2304][] [#1047][] @golopot, @yannickcr)

Readme

Source

ESLint-plugin-React

Maintenance Status NPM version Build Status Dependency Status Coverage Status Code Climate

React specific linting rules for ESLint

Installation

Install ESLint either locally or globally.

$ npm install eslint --save-dev

If you installed ESLint globally, you have to install React plugin globally too. Otherwise, install it locally.

$ npm install eslint-plugin-react --save-dev

Configuration

Use our preset to get reasonable defaults:

  "extends": [
    "eslint:recommended",
    "plugin:react/recommended"
  ]

You should also specify settings that will be shared across all the plugin rules. (More about eslint shared settings)

{
  "settings": {
    "react": {
      "createClass": "createReactClass", // Regex for Component Factory to use,
                                         // default to "createReactClass"
      "pragma": "React",  // Pragma to use, default to "React"
      "version": "detect", // React version. "detect" automatically picks the version you have installed.
                           // You can also use `16.0`, `16.3`, etc, if you want to override the detected value.
                           // default to latest and warns if missing
                           // It will default to "detect" in the future
      "flowVersion": "0.53" // Flow version
    },
    "propWrapperFunctions": [
        // The names of any function used to wrap propTypes, e.g. `forbidExtraProps`. If this isn't set, any propTypes wrapped in a function will be skipped.
        "forbidExtraProps",
        {"property": "freeze", "object": "Object"},
        {"property": "myFavoriteWrapper"}
    ],
    "linkComponents": [
      // Components used as alternatives to <a> for linking, eg. <Link to={ url } />
      "Hyperlink",
      {"name": "Link", "linkAttribute": "to"}
    ]
  }
}

If you do not use a preset you will need to specify individual rules and add extra configuration.

Add "react" to the plugins section.

{
  "plugins": [
    "react"
  ]
}

Enable JSX support.

With ESLint 2+

{
  "parserOptions": {
    "ecmaFeatures": {
      "jsx": true
    }
  }
}

Enable the rules that you would like to use.

  "rules": {
    "react/jsx-uses-react": "error",
    "react/jsx-uses-vars": "error",
  }

List of supported rules

JSX-specific rules

Other useful plugins

Shareable configurations

This plugin exports a recommended configuration that enforces React good practices.

To enable this configuration use the extends property in your .eslintrc config file:

{
  "extends": ["eslint:recommended", "plugin:react/recommended"]
}

See ESLint documentation for more information about extending configuration files.

The rules enabled in this configuration are:

All

This plugin also exports an all configuration that includes every available rule. This pairs well with the eslint:all rule.

{
  "plugins": [
    "react"
  ],
  "extends": ["eslint:all", "plugin:react/all"]
}

Note: These configurations will import eslint-plugin-react and enable JSX in parser options.

License

ESLint-plugin-React is licensed under the MIT License.

Keywords

FAQs

Last updated on 23 Jun 2019

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc