Update React rules #43
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR includes some big updates to our React rules. 🎉
Many of these changes were done by manually merging code from Airbnb. Then I went through each Airbnb rule individually, disabled a few and adjusted the levels and configurations of others. The diff for this is difficult to look through, so I've broken down each rule change below. Please review each one and comment with any thoughts or concerns.
After bringing in the Airbnb changes, I also made some updates to the React README.md to bring it more in line with our rules. Finally, I made a few tweaks to the Docker build based on comments from my previous PR (#42). This would be the first release to use the new build.
Major Updates 💪
eslint-config-hudlnow requires ESLint 4 or 5. This was previously on 3.New Error Rules 🛑
Violating any of these will be counted as an error.
react/jsx-no-duplicate-propsDon't pass the same prop to a component more than once.
react/no-direct-mutation-stateNever assign to
this.statedirectly.react/no-will-update-set-stateNo calls to
this.setState()withincomponentWillUpdate()react/no-is-mountedDon't call
this.isMounted(). Nobody should be doing this at this point anyway.react/prefer-es6-classDon't use
React.createClass(). This was deprecated in React 15.5.react/require-render-returnRender method must always have a
returnstatementreact/no-render-return-valueDon't use the return value of
ReactDOM.render(). Use a ref instead.react/no-comment-textnodesPrevent accidental comments in JSX that get rendered as actual text.
react/no-danger-with-childrenDon't use
dangerouslySetInnerHTMLwhen the component has child contentreact/no-typosPrevent typos when casing common React properties or methods, such as
propTypesorcomponentWillUpdate.react/no-this-in-sfcPrevent
thisfrom being used in a stateless functional component.react/no-access-state-in-setstateThis is an anti-pattern that can be easily avoided.
react/void-dom-elements-no-childrenCertain elements, such as
<br/>,<img/>,<hr/>, should not have children.react/default-props-match-prop-typesDefault props should always be specified in
propTypestoo.react/no-redundant-should-component-updateComponents extending
PureComponentshould not implement ashouldComponentUpdate.react/jsx-props-no-multi-spacesOnly have one space between JSX props.
react/no-children-propChildren should be passed as child elements, not as a
childrenprop.react/no-unused-statePrevent setting state values if that value is never read.
react-hooksEnforce React hooks rules.
Fixable Errors 🔧
Each of these rules will also produce an error, but can be fixed automatically by running ESLint with the
--fixparameter. These are all related to JSX formatting.jsx-quotesRequire double quotes in JSX attributes.
react/jsx-equals-spacingThere should be no spaces around JSX equals signs, such as:
<Hello name = {firstname} /><Hello name ={firstname} /><Hello name= {firstname} />react/jsx-indentJSX should be indented with 2 spaces.
react/jsx-closing-tag-locationClosing tags should be aligned with or on the same line as the opening tag.
react/jsx-tag-spacingPrevent spaces in JSX brackets, such as:
< Hello></Hello><Hello>< /Hello><Hello></Hello >A trailing space in a self-closing tag is okay:
<Hello />Updated rules ♻️
react/sort-compWe were previously enforcing an order of:
static-methodslifecycleeverything-elserenderThis PR adds two new cases to this rule:
static-methods, but still before the constructor or lifecycle methodsrender*method must go at the bottom, before the finalrender()New Warnings⚠️
These are the more egregious issues that may be much more involved to fix. If these were full errors, it may make it difficult to update a repository to the new ESLint rules without having to disable several lines and then forgetting about those violations later. For that reason I have them configured as warnings, but I'm open to any pushback on this.
react/jsx-no-bindWarn for new functions that are passed as props. This could be a
function(), an arrow, or use of.bind(). This is an anti-pattern.react/no-string-refsUsing strings as refs is considered a React legacy feature.
react/no-find-dom-nodeDon't use
findDOMNode(). This is an anti-pattern.react/no-array-index-keyKeys should be based on an item property rather than their index in the array. This is an anti-pattern.