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

Cannot read property 'type' of null error in no-array-index-key.js #1242

Closed
alaycock opened this issue Jun 6, 2017 · 4 comments
Closed

Cannot read property 'type' of null error in no-array-index-key.js #1242

alaycock opened this issue Jun 6, 2017 · 4 comments

Comments

@alaycock
Copy link

alaycock commented Jun 6, 2017

Version 7.0.1
The full stack trace can be found below.

screen shot 2017-06-06 at 1 20 23 pm

The issue occurs when I begin to add the key to this code:

      this.state.array.map((arrayItem, index) => (
        <div>
          <button onClick={() => this.delete(index)}>Delete this thing</button>
        </div>
      ))

If I begin to enter the key attribute:

      this.state.array.map((arrayItem, index) => (
        <div key>
          <button onClick={() => this.delete(index)}>Delete this thing</button>
        </div>
      ))

An error in the console is triggered, although I wasn't finished writing the new line.

The issue appears to be on this line, and I suspect it could be corrected altering the line to be if (!value || value.type !== 'JSXExpressionContainer') {, but I'm not sure about the implication of making that change.

@ljharb
Copy link
Member

ljharb commented Jun 6, 2017

So, setting aside "errors while you're typing", which isn't something that needs supporting, it looks like <div key />inside a .map throws this error?

@alaycock
Copy link
Author

alaycock commented Jun 7, 2017

Exactly. It looks like the variable which holds the value is null, which throws the TypeError.

@jseminck
Copy link
Contributor

jseminck commented Jun 7, 2017

Confirmed by adding a test case:

  1) no-array-index-key invalid foo.map((bar, i) => <Foo key />):
     TypeError: Cannot read property 'type' of null

But... should it be valid, or invalid? According to this specific rule I guess it should be valid, since it's not assigning the index to the key... but then again, just having foo.map((bar, i) => <Foo key />) seems wrong?

@ljharb
Copy link
Member

ljharb commented Jun 7, 2017

In that case, it's setting the key to true - which is not a use of the index. Separately, it's making the key be the same, which defeats the purpose - but ensuring the key is unique across items is something that React could check at runtime, not something a linter rule could reliably enforce.

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

No branches or pull requests

3 participants