Skip to content

Commit 7ae3146

Browse files
EivindArveseniansu
authored andcommitted
Support React Hooks (#5602) (#5997)
* Add Babel plug-in for Hooks; Add ESLint plug-in for Hooks (WIP) * Fix transform destructuring loose config * Add eslint-plugin-react-hooks to package.json * Fix package.json's * Fix eslint-plugin-react-hooks version * Fix package.json * Fix package.json * Add eslint-plugin-react-hooks to script package.json * Force array destructuring to work in loose mode only for known Hooks * Update based on feedback from PR * Add 'exhaustive-deps' lint rule * Bump eslint-plugin-react-hooks to stable version * Remove extraneous dependency from react-scripts
1 parent f4f20a2 commit 7ae3146

File tree

5 files changed

+29
-4
lines changed

5 files changed

+29
-4
lines changed

packages/babel-preset-react-app/create.js

+20-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,26 @@ module.exports = function(api, opts, env) {
117117
// Necessary to include regardless of the environment because
118118
// in practice some other transforms (such as object-rest-spread)
119119
// don't work without it: https://github.com/babel/babel/issues/7215
120-
require('@babel/plugin-transform-destructuring').default,
120+
[
121+
require('@babel/plugin-transform-destructuring').default,
122+
{
123+
// Use loose mode for performance:
124+
// https://github.com/facebook/create-react-app/issues/5602
125+
loose: false,
126+
selectiveLoose: [
127+
'useState',
128+
'useEffect',
129+
'useContext',
130+
'useReducer',
131+
'useCallback',
132+
'useMemo',
133+
'useRef',
134+
'useImperativeHandle',
135+
'useLayoutEffect',
136+
'useDebugValue',
137+
],
138+
},
139+
],
121140
// Turn on legacy decorators for TypeScript files
122141
isTypeScriptEnabled && [
123142
require('@babel/plugin-proposal-decorators').default,

packages/eslint-config-react-app/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ If you want to use this ESLint configuration in a project not built with Create
1919
First, install this package, ESLint and the necessary plugins.
2020

2121
```sh
22-
22+
2323
```
2424

2525
Then create a file named `.eslintrc.json` with following contents in the root folder of your project:

packages/eslint-config-react-app/index.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ module.exports = {
2828

2929
parser: 'babel-eslint',
3030

31-
plugins: ['import', 'flowtype', 'jsx-a11y', 'react'],
31+
plugins: ['import', 'flowtype', 'jsx-a11y', 'react', 'react-hooks'],
3232

3333
env: {
3434
browser: true,
@@ -161,6 +161,7 @@ module.exports = {
161161
],
162162
'no-with': 'warn',
163163
'no-whitespace-before-property': 'warn',
164+
'react-hooks/exhaustive-deps': 'warn',
164165
'require-yield': 'warn',
165166
'rest-spread-spacing': ['warn', 'never'],
166167
strict: ['warn', 'never'],
@@ -241,6 +242,9 @@ module.exports = {
241242
'jsx-a11y/role-supports-aria-props': 'warn',
242243
'jsx-a11y/scope': 'warn',
243244

245+
// https://github.com/facebook/react/tree/master/packages/eslint-plugin-react-hooks
246+
'react-hooks/rules-of-hooks': 'error',
247+
244248
// https://github.com/gajus/eslint-plugin-flowtype
245249
'flowtype/define-flow-type': 'warn',
246250
'flowtype/require-valid-file-annotation': 'warn',

packages/eslint-config-react-app/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
"eslint-plugin-flowtype": "2.x",
1717
"eslint-plugin-import": "2.x",
1818
"eslint-plugin-jsx-a11y": "6.x",
19-
"eslint-plugin-react": "7.x"
19+
"eslint-plugin-react": "7.x",
20+
"eslint-plugin-react-hooks": "1.5.0"
2021
},
2122
"dependencies": {
2223
"confusing-browser-globals": "^1.0.6"

packages/react-scripts/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
"eslint-plugin-import": "2.16.0",
4545
"eslint-plugin-jsx-a11y": "6.2.1",
4646
"eslint-plugin-react": "7.12.4",
47+
"eslint-plugin-react-hooks": "1.5.0",
4748
"file-loader": "3.0.1",
4849
"fs-extra": "7.0.1",
4950
"html-webpack-plugin": "4.0.0-beta.5",

0 commit comments

Comments
 (0)