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: A props object containing a "key" prop is being spread into JSX: #600

Closed
MoSattler opened this issue Apr 24, 2024 · 4 comments
Closed

Comments

@MoSattler
Copy link

Describe the bug and the expected behavior

I am getting a console error in Next when using conforms getInputProps.

Conform version

1.1.2

Steps to Reproduce the Bug or Issue

<input {...getInputProps(fields.id, { type: 'hidden' })} />

I get the error message

Warning: A props object containing a "key" prop is being spread into JSX:
  let props = {key: someKey, id: ..., name: ..., form: ..., type: ...};
  <input {...props} />
React keys must be passed directly to JSX without using spread:
  let props = {id: ..., name: ..., form: ..., type: ...};
  <input key={someKey} {...props} />

What browsers are you seeing the problem on?

No response

Screenshots or Videos

No response

Additional context

No response

@edmundhung
Copy link
Owner

You can remove the warning by explicitly setting the key like this:

<input {...getInputProps(fields.id, { type: 'hidden' })} key={fields.id.key} />

The react team seems to against spreading an object with key on the element and likely adding this warning to react 18.3 as well 😅 There is nothing I can really do because the whole point of spreading an object is to minimise the need to setting each property yourself. If they want you to be explicit, then we have to be explicit...

@MoSattler
Copy link
Author

Would it be sensible to omit the key from getInputProps then?

@edmundhung
Copy link
Owner

I would say no. Although the key is needed only for some features, it is better to have it set then realize it wasn't working later 😅 Maybe consider creating your own Input component so you don't need to repeat this manually.

@MoSattler
Copy link
Author

Fair enough! I was worried that this might eventually cause issues with React (otherwise, why would they warn about it?), and that console errors might trip people up.

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

No branches or pull requests

2 participants