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
False positive display-name when using forwardRef #2269
Comments
hmm, the inner function is not in fact a component, because it takes |
I have pretty the same issue with React.memo, and as it was explained, I did the same for forwardRef - and error disappeared. import PropTypes from 'prop-types'
import React from 'react'
function Paragraph({ style }, ref) {
return (<div ref={ref} style={style} />)
}
Paragraph.propTypes = {
style: PropTypes.object
};
export default React.forwardRef(Paragraph) |
Oh, ok. In same ways it makes sense. Any idea about why the Object.assign stops the detection? |
A forwardRef callback is not a component and shouldn’t have propTypes; a component doesn’t get a ref argument. |
Well, then... What is the expected way to deal with it? Could you provide a code sample? |
import PropTypes from 'prop-types';
import React from 'react';
const Paragraph = React.forwardRef(({ style }, ref) => {
const finalStyle = Object.assign({}, style);
return <div ref={ref} style={finalStyle} />;
});
Paragraph.displayName = 'Paragraph';
Paragraph.propTypes = {
style: PropTypes.object
};
export default Paragraph;
export const proptype = Paragraph.PropTypes; |
From rule motivation:
React uses So the fact that this function is not a component doesn't mean that its name (or even |
Fair, but this rule is about component display names. |
In the provided case, the |
It gets one regardless of the name of the callback function tho, right? |
If the callback function has no Here, |
I completely agree with @Hypnosphi, without a Simple example: Before upgrading to Unicorn, I just had:
Was erroring out (for a reason not shown above), and was just showing me
|
@davidwieler the question is, whether the following work as well. I say that the following is just as good it terms of debugging, note the named function:
Can you please check it in your setup if you have any doubts? |
Regardless of how it looks when debugging, this is a false positive. It asks for the function passed to This is not a problem of just this rule, but a conceptual problem of how these rules detect React components. I'm getting similar false positives for both |
@hon2a react function components accept 2 arguments already, props and context. |
However, certainly if React gives a runtime warning about having a displayName in certain cases, this rule shouldn’t be suggesting it then. Would you mind opening up a PR, with test cases? |
@ljharb Could you, please, point me to any documentation stating that? Aren't you confusing it with constructor contract for class components?
I can look into creating simple test cases. If by PR you mean with a fix as well, that I can't promise, at least not at this time :) |
It’s how SFCs have worked since their inception, in React 0.14. I can dig up documentation later; but i have production code in use for years that relies on it. Test cases would be great :-) |
I guess it's been too long since I used legacy context. Funny thing is, I can't find a mention of that anywhere. |
It’s also possible it’s been undocumented since new context came out ¯\_(ツ)_/¯ it still works tho |
This is supposed to be fixed right? I'm on 7.16.0 and still getting a false positive here: /**
* @flow
* @prettier
*/
import * as React from 'react'
import classNames from 'classnames'
export type Props = {
+className: string,
+children: React.Element<any>,
}
const CloneWithClassName = React.forwardRef(
({ className, children }: Props, ref: React.ElementRef<any>): React.Node => {
const child = React.Children.only(children)
return React.cloneElement(child, {
ref,
className: classNames(child.props.className, className),
})
}
)
export default CloneWithClassName |
@jedwards1211 it's fixed in master but unreleased. |
okay thanks, I guess there are other false positive fixes in the changelog |
@jedwards1211 https://github.com/yannickcr/eslint-plugin-react/blob/master/CHANGELOG.md is accurate; the git log certainly might contain unreleased stuff in any repo :-) |
|
Wrapping a named function declaration with a React.memo or React.forwardRef will no longer throw an false positive error Fixes jsx-eslint#2324. Fixes jsx-eslint#2269.
Tell us about your environment
What parser (default, Babel-ESLint, etc.) are you using? babel-eslint 10.0.1
Please show your full configuration:
Configuration
What did you do? Please include the actual source code causing the issue, as well as the command that you used to run ESLint.
What did you expect to happen?
Nothing as the file is valid
What actually happened? Please include the actual, raw output from ESLint.
Fun fact! If you uncomment the line with Object.assign, the false positive disappears...
The text was updated successfully, but these errors were encountered: