Skip to content

Add eslint rule to error on reads or writes to refs in render #24506

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
239 changes: 239 additions & 0 deletions packages/eslint-plugin-react-hooks/__tests__/ESLintPureRender-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,239 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

'use strict';

const ESLintTester = require('eslint').RuleTester;
const ReactHooksESLintPlugin = require('eslint-plugin-react-hooks');
const PureRenderESLintRule = ReactHooksESLintPlugin.rules['pure-render'];

ESLintTester.setDefaultConfig({
parser: require.resolve('babel-eslint'),
parserOptions: {
ecmaVersion: 6,
sourceType: 'module',
},
});

const writeError = {
message:
'Writing to refs during rendering is not allowed. Move this into a useEffect or useLayoutEffect. See https://beta.reactjs.org/apis/useref',
};
const readError = {
message:
'Reading from refs during rendering is not allowed. See https://beta.reactjs.org/apis/useref',
};
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like some help with these error messages. I'm not sure exactly how much info we'd like to expose in the message vs in the docs link.

Also, would be nice if we could link directly to the "Pitfall" heading on that docs page, but right now it doesn't seem to have an anchor link.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good job.


const tests = {
valid: [
{
code: `
function MyComponent() {
let ref = useRef(false);
useLayoutEffect(() => {
ref.current = true;
});
}
`,
},
{
code: `
let MyComponent = () => {
let ref = useRef(false);
useLayoutEffect(() => {
ref.current = true;
});
};
`,
},
{
code: `
let MyComponent = function () {
let ref = useRef(false);
useLayoutEffect(() => {
ref.current = true;
});
};
`,
},
{
code: `
function MyComponent() {
let ref = useRef(false);
let onChange = () => {
ref.current = true;
};
}
`,
},
{
code: `
function MyComponent() {
let ref = useRef(null);
if (ref.current == null) {
ref.current = somethingExpensive();
}
}
`,
},
{
code: `
function MyComponent() {
let ref = useRef(false);
if (ref.current === false) {
ref.current = somethingExpensive();
}
}
`,
},
{
code: `
function MyComponent() {
let ref = useRef(undefined);
if (ref.current === undefined) {
ref.current = somethingExpensive();
}
}
`,
},
{
code: `
function MyComponent() {
let ref = useRef(undefined);
if (ref.current == null) {
ref.current = somethingExpensive();
}
}
`,
},
{
code: `
function MyComponent() {
let ref = useRef();
if (ref.current == null) {
ref.current = somethingExpensive();
}
}
`,
},
{
code: `
function MyComponent() {
let ref = useRef();
if (ref.current == null) {
ref.current = somethingExpensive();
}
}
`,
},
{
code: `
function MyComponent() {
let ref = React.useRef();
if (ref.current == null) {
ref.current = somethingExpensive();
}
}
`,
},
],
invalid: [
{
code: `
function MyComponent() {
let ref = useRef(false);
ref.current = true;
}
`,
errors: [writeError],
},
{
code: `
let MyComponent = () => {
let ref = useRef(false);
ref.current = true;
};
`,
errors: [writeError],
},
{
code: `
let MyComponent = function () {
let ref = useRef(false);
ref.current = true;
};
`,
errors: [writeError],
},
{
code: `
function MyComponent() {
let ref = React.useRef(false);
ref.current = true;
}
`,
errors: [writeError],
},
{
code: `
function MyComponent() {
let ref = useRef(false);
return <h1>{ref.current}</h1>;
}
`,
errors: [readError],
},
{
code: `
function MyComponent() {
let ref = useRef(false);
if (ref.current === 'test') {
ref.current = somethingExpensive();
}
}
`,
errors: [readError, writeError],
},
{
code: `
function MyComponent() {
let ref = useRef(null);
if (ref.current === undefined) {
ref.current = somethingExpensive();
}
}
`,
errors: [readError, writeError],
},
{
code: `
function MyComponent() {
let ref = useRef(undefined);
if (ref.current === null) {
ref.current = somethingExpensive();
}
}
`,
errors: [readError, writeError],
},
{
code: `
function MyComponent() {
let ref = useRef(null);
let someOtherRef = useRef(null);
if (someOtherRef.current === null) {
ref.current = somethingExpensive();
}
}
`,
errors: [writeError],
},
],
};

const eslintTester = new ESLintTester();
eslintTester.run('react-hooks', PureRenderESLintRule, tests);
Loading