-
Notifications
You must be signed in to change notification settings - Fork 0
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
feat(eslint-config): new rulе no-modifying-ref-on-render #37
Conversation
function reportError(context, node) { | ||
context.report({ | ||
node, | ||
message: 'Do not modifying ref on render', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Давай как в доке - Do not write ref.current during rendering
https://react.dev/reference/react/useRef#useref:~:text=Next%20Example-,Pitfall,-Do%20not%20write
return <div>text</div>; | ||
}`, | ||
}, | ||
], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
copilot подсказал
{
code: `
const MyComponent = (props) => {
const ref = useRef(props);
return <div>text</div>;
}`,
},
{
code: `
const MyComponent = (props) => {
const ref = useRef(props);
useEffect(() => {
// Do something
}, []);
return <div>text</div>;
}`,
},
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
{
code: `
const MyComponent = (props) => {
const ref = useRef(props);
useEffect(() => {
ref.current = props;
}, [props]);
return <div>text</div>;
}`,
},
}`, | ||
errors: [errorMsg()], | ||
}, | ||
], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
а это вроде уже сложно?
{
code: `
const MyComponent = (props) => {
const ref = useRef(props);
useEffect(() => {
ref.current = props;
}, []);
return <div>text</div>;
}`,
},
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ту не понял. Код валидный.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ref-ку надо обычно обновлять на каждый рендер. А в данном случае обновляется только на первый рендер - это не имеет смысла, так как на первый рендер мы и так кладем значение через useRef. Но давай забьем, это хитрый кейс - вряд ли кто-то так пишет. Это мои фантазии )
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ага ясно, пустые зависимости в useEffect. Да действительно редкий случай.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.
a6227cc
to
b3a6283
Compare
📦 Published PR as canary version:
Canary Versions
✨ Test out this PR locally via: