UnusedownProps
parameter of the React ReduxmapStateToProps
function should be removed
This rule applies when the secondownProps
parameter of the React ReduxmapStateToProps
function is not used.
ThemapStateToProps(state, ownProps)
is specified as the first argument ofconnect()
call and itsownProps
parameter receives the props object of the wrapper component. If theownProps
parameter is not present, React Redux skips calling the function at the props change.
Therefore, for performance, it is recommended to remove theownProps
parameter if it is not used in the function body. In some cases, such modification saves not only the function execution time but also unnecessary re-renderings.
This rule also applies to themapDispatchToProps
which takesownProps
parameter in a similar way.
Noncompliant Code Example | Compliant Code Example | ||
---|---|---|---|
1 | import React from 'react'; | 1 | import React from 'react'; |
2 | import { connect } from 'react-redux'; | 2 | import { connect } from 'react-redux'; |
3 | 3 | ||
4 | function ShowCount(props) { | 4 | function ShowCount(props) { |
5 | return <div>count: {props.count}</div>; | 5 | return <div>count: {props.count}</div>; |
6 | } | 6 | } |
7 | const mapStateToProps = (state, ownProps) => { // REACT_REDUX_UNUSED_OWN_PROPS_PARAM alarm | 7 | const mapStateToProps = (state) => { |
8 | return { count: state.count }; | 8 | return { count: state.count }; |
9 | } | 9 | } |
10 | export default connect(mapStateToProps)(ShowCount); | 10 | export default connect(mapStateToProps)(ShowCount); |
import React from 'react';import { connect } from 'react-redux';function ShowCount(props) { return <div>count: {props.count}</div>;}const mapStateToProps = (state, ownProps) => { // REACT_REDUX_UNUSED_OWN_PROPS_PARAM alarm return { count: state.count };}export default connect(mapStateToProps)(ShowCount);
import React from 'react';import { connect } from 'react-redux';function ShowCount(props) { return <div>count: {props.count}</div>;}const mapStateToProps = (state) => { return { count: state.count };}export default connect(mapStateToProps)(ShowCount);
This rule was introduced in DeepScan 1.26.0.