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

Latest commit

 

History

History
55 lines (39 loc) · 1.24 KB

mapStateToProps-no-store.md

File metadata and controls

55 lines (39 loc) · 1.24 KB

Enforces that mapStateToProps does not bind complete store to a component. (react-redux/mapStateToProps-no-store)

Passing whole state to a component is a bad practice, triggering unnecessary re-renders. Additionally bad is passing around a mutable object that your component critically depends on preventing mutations to. Instead one should specify the properties actually used by a component.

Rule details

The following patterns are considered incorrect:

const mapStateToProps = (state) => state
const mapStateToProps = state => {
        return {state: state}
      }
const mapStateToProps = state => ({...state});
connect((state) => state, null)(App)

The following patterns are correct:

const mapStateToProps = () => {}
const mapStateToProps = (state) => {isActive: state.isActive}
const mapStateToProps = ({isActive}) => {isActive}
connect((state) => ({isActive: state.isActive}), null)(App)

Not supported use cases.

Please note that the following use case, although common, is not supported due to the nature of static code analysis.

The following would not warn:

const getProps = (state) => state;
const mapStateToProps = (state) => getProps(state);