I'm working through the discipline of using ESLint (Airbnb config) with React / Redux.
The following code is a standard type React / Redux class that I've written to enjoy dealing with all of Airbnb's linting discipline.
Most of the linting errors I've resolved by understanding what Airbnb's configuration prefers, however, there are a couple of areas that I presently don't understand. These are:
- The entire method renderTableHeader() is outlined in red and ESLint tells me:
[eslint] Expected 'this' to be used by class method 'renderTableHeader'. (class-methods-use-this) (JSX attribute) className: string
none of the other methods have this linting problem
- I connect an object from Redux state to props (contains many key/objects that I need to iterate over in this class). ESLint doesn't seem to like me wiring objects from state to props...giving me the message:
[eslint] Prop type
object
is forbidden (react/forbid-prop-types) import PropTypes
My state object is an object containing many keys that reference user objects - so I need users
to be an object. Is this bad practice? If it is how would I connect an object to map over without this lint message appearing?
Thanks in advance...here's my class:
import React, { Component } from 'react';
import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';
import PropTypes from 'prop-types';
import _ from 'lodash';
import { getUsers } from '../actions/getUsers';
import setUserMarketingPref from '../actions/setUserMarketingPref';
import { setFilter } from '../actions/setFilter';
import TableRow from '../components/TableRow';
import '../styles/styles.css';
class AppContainer extends Component {
componentDidMount() {
this.props.getUsers();
}
renderTableHeader() {
return (
<div className="table__header">
<div className="table__header--name">Name</div>
<div className="table__header--gender">Gender</div>
<div className="table__header--region">Region</div>
</div>
);
}
renderTable() {
const { users, filterMode } = this.props;
const usersView = filterMode ? _.omitBy(users, user => !user.checked) : users;
return _.map(usersView, user => (
<TableRow
user={user}
key={user.id}
setUserMarketingPref={_.debounce(() => this.props.setUserMarketingPref(user.id), 100)}
/>
));
}
renderFilters() {
return (
<div className="table__buttons">
<button
id="marketing-other"
className="table__other"
onClick={e => this.props.setFilter(e.target.id)}
>Other filter
</button>
<button
id="marketing-none"
className="table__no-marketing"
onClick={e => this.props.setFilter(e.target.id)}
>No marketing
</button>
</div>
);
}
render() {
if (!_.size(this.props.users)) {
return null;
}
return (
<div className="table__container">
{this.renderTableHeader()}
{this.renderTable()}
{this.renderFilters()}
</div>
);
}
}
const mapDispatchToProps = dispatch => bindActionCreators(
{
getUsers,
setUserMarketingPref,
setFilter,
},
dispatch,
);
const mapStateToProps = state => ({
users: state.users,
filter: state.filter,
filterMode: state.marketing.filterMode,
});
AppContainer.defaultProps = {
filterMode: false,
getUsers: null,
setUserMarketingPref: null,
setFilter: null,
};
AppContainer.propTypes = {
users: PropTypes.object,
filterMode: PropTypes.bool,
getUsers: PropTypes.func,
setUserMarketingPref: PropTypes.func,
setFilter: PropTypes.func,
};
export default connect(mapStateToProps, mapDispatchToProps)(AppContainer);
this
would likely lead to it complaining aboutrenderTable()
. I think they want the shape of the object defined, or you can just disable the line. It's up to you. reactjs.org/docs/typechecking-with-proptypes.html – sesamechicken