[Feature Request][eslint-plugin-react-hooks] no-ref-checks, display error when using useRef's return value as condition #22168
Description
reactjs/rfcs#198
jsx-eslint/eslint-plugin-react#3042
Not sure whether this belongs in eslint-plugin-react-hooks
or eslint-plugin-react
, but a contributor at the latter suggested this to be a more appropriate place. I've opened an issue instead of an RFC to discuss whether this is the appropriate place. jsx-eslint/eslint-plugin-react#3042 (comment)
I started migrating a codebase from class-based to function-based and came across some silliness. I had completely missed converting some of the ref checks:
- const thing = this.thing;
+ const thing = useRef(props.thing);
// I missed this
- if (!thing) {
+ if (!thing.current) {
TypeScript considers !useRef(props.thing)
to be perfectly valid since it may be testing for non-existence of the value even if it is not boolean. However, due to the rules of hooks, this value will never be undefined -- any checks for it are unnecessary and could either be an innocuous useless check or indicate a serious bug / typo. Thus, I'm thinking it might make sense to make sure a useRef's return value is never used as a boolean or condition at the react linter level. Not sure if it's possible with ESLint, but if so it could be a very useful rule which would catch a lot of bugs.