Skip to content
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

Warning if React.forwardRef render function doesn't take exactly two arguments is confusing when more than 2 arguments #13627

Closed
andresroberto opened this issue Sep 12, 2018 · 13 comments

Comments

@andresroberto
Copy link
Contributor

Do you want to request a feature or report a bug?

A feature (an improvement)

What is the current behavior?

When defining more than two parameters for a React.forwardRef render function a warning message stating "forwardRef render functions accept two parameters: props and ref. Did you forget to use the ref parameter?" gets logged.

If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem. Your bug will get fixed much faster if we can run your code and it doesn't have dependencies other than React. Paste the link to your JSFiddle (https://jsfiddle.net/Luktwrdm/) or CodeSandbox (https://codesandbox.io/s/new) example below:

https://codesandbox.io/s/5v704qxvnx

What is the expected behavior?

Option 1

Overall the message could be more explicit on accepting exactly two parameters. Also, when the arity is greater than 2 the second part of the message may be omitted. For instance:

""forwardRef render functions accept exactly two parameters: props and ref"

Option 2

If defining more than 2 arguments to the function is ok, which may be the case since the real goal for this warning is just to make sure people use the ref parameter, I think the message could not be shown for arity > 2, therefore chaging the condition of === 2 to > 1.

Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?

16.5.0 which is the one that includes this new warning

@gaearon
Copy link
Collaborator

gaearon commented Sep 12, 2018

Happy to take a PR for “option 1”.

The purpose of accepting more arguments is not clear to me because they’d never get filled in.

@andresroberto
Copy link
Contributor Author

Cool. I will make a PR tonight

@jquense
Copy link
Contributor

jquense commented Sep 12, 2018

We might want to add a note about an argument rest as well? forwardRef((...args) => render(...args)) also warning in a confusing manor since it's transpiled to use arguments directly

@gaearon
Copy link
Collaborator

gaearon commented Sep 12, 2018

I think it shouldn't warn about 0 arguments.

gaearon pushed a commit that referenced this issue Sep 13, 2018
* Enhance dev warnings for forwardRef render function

- For 0 parameters: Do not warn because it may be due to usage of the
  arguments object.

- For 1 parameter: Warn about missing the 'ref' parameter.

- For 2 parameters: This is the ideal. Do not warn.

- For more than 2 parameters: Warn about undefined parameters.

* Make test cases for forwardRef warnings more realistic

* Add period to warning sentence
@gaearon
Copy link
Collaborator

gaearon commented Sep 13, 2018

Should be fixed in 16.5.1

@gaearon gaearon closed this as completed Sep 13, 2018
Simek pushed a commit to Simek/react that referenced this issue Oct 25, 2018
…acebook#13636)

* Enhance dev warnings for forwardRef render function

- For 0 parameters: Do not warn because it may be due to usage of the
  arguments object.

- For 1 parameter: Warn about missing the 'ref' parameter.

- For 2 parameters: This is the ideal. Do not warn.

- For more than 2 parameters: Warn about undefined parameters.

* Make test cases for forwardRef warnings more realistic

* Add period to warning sentence
jetoneza pushed a commit to jetoneza/react that referenced this issue Jan 23, 2019
…acebook#13636)

* Enhance dev warnings for forwardRef render function

- For 0 parameters: Do not warn because it may be due to usage of the
  arguments object.

- For 1 parameter: Warn about missing the 'ref' parameter.

- For 2 parameters: This is the ideal. Do not warn.

- For more than 2 parameters: Warn about undefined parameters.

* Make test cases for forwardRef warnings more realistic

* Add period to warning sentence
@sachbazjan
Copy link

This warning is still visible in react 17.0.1

@enoh-barbu
Copy link

enoh-barbu commented Jun 13, 2022

I've just got this error after running npm update. (React v17)

@enoh-barbu
Copy link

Just put a debugger and saw that these errors ar comming from MUI

@momolly1024
Copy link

Just put a debugger and saw that these errors ar comming from MUI

Me too

@gvanriper
Copy link

Just put a debugger and saw that these errors ar comming from MUI

Yes, I am reporting this issue as well. Will create a ticket for MUI if there isn't one already.

@gvanriper
Copy link

gvanriper commented Jun 15, 2022

Hey all, if you're receiving this from MUI, do you happen to be using the x-date-picker imported from mui/lab? If so, switch to the separated mui/x-date-picker npm package.

The date picker from mui/lab has been deprecated and it's improperly using forwardRef:

Screen Shot 2022-06-15 at 10 04 42 AM

@enoh-barbu
Copy link

@gvanriper yes, that was the reason, already switched. Thanks for letting others know

@ubRoman
Copy link

ubRoman commented Nov 17, 2022

Hello all,
in my own component (Reactjs+TypeScript) I want to use forwarded ref optionally.
I have achieved this with a default value - it works fine. The second param looks like this:

const myComp = React.forwardRef(... , ref: React.ForwardedRef<HTMLDivElement> = useRef<HTMLDivElement>(null))

Unfortunatelly in browser Im getting this irritating warning, even if I pass the ref from the top component.
Can I suppress the warning or is there another solution with TypeScript in such cases?

Especially the warning is very annoying in jest logs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants