Eslint-plugin-react: Memo + ForwardRef = false positive?

Created on 16 Jul 2019  路  6Comments  路  Source: yannickcr/eslint-plugin-react

Eslint complains about myProp being missing in prop validation
when a forwardRef'd component is memoized.

const myComponent = memo(forwardRef( ({ myProp }, ref) => {
....
}

If I add myProp to prop validation.

const myComponent = ({ myProp }, ref) => {
....
}
myComponent.propTypes = { myProp: PropTypes.number.isRequired }
default export memo(forwardRef(myComponent));

Then eslint stops complaining, but now the site wont render:

Warning: forwardRef render functions do not support propTypes or defaultProps. Did you accidentally pass a React component?

Is there someway to use memo and forwardRef that eslint doesn't complain about?

bug help wanted

All 6 comments

forwardRef callbacks aren鈥檛 components and don鈥檛 take props or propTypes - if you have a forwardRef, why isn't it using the ref argument?

forwardRef callbacks aren鈥檛 components and don鈥檛 take props or propTypes - if you have a forwardRef, why isn't it using the ref argument?

Whoops, you're right, forgot the 2nd param "Ref" for this example. Fixed.

I realized that forwardRef'd components don't have propTypes, what I'm saying is Eslint complains if I don't validate props using propTypes when I have a memoized forwardRef. If fowardRef doesn't have propTypes then a memoized forwardRef shouldnt either, yet eslint want's me to add propTypes to it.
Hence why i think this is a false positive.

If it only happens with the combination - ie, with memo and forwardRef, but not just with forwardRef - then it鈥檚 definitely a bug.

If it only happens with the combination - ie, with memo and forwardRef, but not just with forwardRef - then it鈥檚 definitely a bug.

That's what i thought.

Having multiple wrapper was not supported, it should be fixed in v7.14.3. Thanks for the report 馃檪

Also you must add the propTypes to the final component:

const myComponent =  memo(forwardRef(({ myProp }, ref) => {
  // ...
}))
myComponent.propTypes = { myProp: PropTypes.number.isRequired }
default export myComponent;

Thanks for the report

Thanks for the fix!

Was this page helpful?
0 / 5 - 0 ratings