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

jsx-key: does not support spread attributes #613

Closed
wmertens opened this issue May 24, 2016 · 8 comments
Closed

jsx-key: does not support spread attributes #613

wmertens opened this issue May 24, 2016 · 8 comments

Comments

@wmertens
Copy link

If you do <SomeEl {...{key: 123}}/>, react/jsx-key will falsely alert.

@TrevorSayre
Copy link

TrevorSayre commented Aug 20, 2019

This is especially problematic if you're using Downshift

(Simplified example)

<Downshift>
  {({ getItemProps, isOpen }) => (
    <ul>
      {isOpen
        ? items.map((item, index) => (
            <li
              {...getItemProps({
                key: item.value,
                index,
                item,
              })}
            >
              {item.value}
            </li>
          ))
        : null
      }
    </ul>
  )}
</Downshift>

You would either disable the rule specifically for this, or you would have to rewrite as:

<Downshift>
  {({ getItemProps, isOpen }) => (
    <ul>
      {isOpen
        ? items.map((item, index) => {
            const props = getItemProps({
              key: item.value,
              index,
              item,
            });

            return (
              <li
                key={props.key}
                {...props}
              >
                {item.value}
              </li>
            )
          })
        : null
      }
    </ul>
  )}
</Downshift>

This feels very clunky.

@EECOLOR
Copy link

EECOLOR commented Oct 19, 2019

There is an rfc that states: "Deprecate spreading key from objects."

#1753
reactjs/rfcs#107

@ljharb
Copy link
Member

ljharb commented Oct 19, 2019

Indeed; key and ref should both always be explicit.

Closing this as won’t fix.

@ljharb ljharb closed this as completed Oct 19, 2019
@ljharb ljharb added the wontfix label Oct 19, 2019
@EECOLOR
Copy link

EECOLOR commented Oct 19, 2019

@ljharb The RFC seems to hint that ref is not restricted and will become a regular prop.

@ljharb
Copy link
Member

ljharb commented Oct 19, 2019

interesting. either way, key won’t be a normal one :-)

@mrchantey
Copy link

mrchantey commented Jun 8, 2022

Are there any good resources out there for understanding why its important to not spread key? The react docs explain the issues with spread in general, but not why keys are different.

@ljharb
Copy link
Member

ljharb commented Jun 9, 2022

Because key is a special prop for React only that components can’t ever receive.

@mrchantey
Copy link

Thank you! that totally makes sense now.

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

No branches or pull requests

5 participants