Skip to content
This repository has been archived by the owner on Oct 21, 2022. It is now read-only.

Latest commit

 

History

History
59 lines (45 loc) · 1.58 KB

useSelector-prefer-selectors.md

File metadata and controls

59 lines (45 loc) · 1.58 KB

Enforces that all useSelector hooks use named selector functions. (react-redux/useSelector-prefer-selectors)

Using selectors in useSelector to pull data from the store or compute derived data allows you to decouple your containers from the state architecture and more easily enable memoization. This rule will ensure that every hook utilizes a named selector.

Rule details

The following pattern is considered incorrect:

const property = useSelector((state) => state.property)
const property = useSelector(function (state) { return state.property })

The following patterns are considered correct:

const selector = (state) => state.property

function Component() {
  const property = useSelector(selector)
  // ...
}

Rule Options

...
"react-redux/useSelector-prefer-selectors": [<enabled>, {
  "matching": <string>
  "validateParams": <boolean>
}]
...

matching

If provided, validates the name of the selector functions against the RegExp pattern provided.

    // .eslintrc
    {
        "react-redux/useSelector-prefer-selectors": ["error", { matching: "^.*Selector$"}]
    }

    // container.js
    const propertyA = useSelector(aSelector) // success
    const propertyB = useSelector(selectB) // failure
    // .eslintrc
    {
        "react-redux/mapStateToProps-prefer-selectors": ["error", { matching: "^get.*FromState$"}]
    }

    // container.js
    const propertyA = useSelector(getAFromState) // success
    const propertyB = useSelector(getB) // failure