12

I'm getting the warning Each child in an array or iterator should have a unique "key" prop. Check the render method of Login

I want to pass an array of elements back, without using keys. I have to believe there's a workaround for this, without adding a pointless wrapper?

Note the return [<div/>, <div/>];

  render () {
    return (
      <div className='login'>
        {this.mobileWeb()}
        {this.pcWeb()}
      </div>
    );
  }

  mobileWeb () {
    if (this.state.isMobileWeb === true) {
      return [
        <div className='sky-container'></div>,
        <div className='sea-container'></div>
      ];
    }
  }

  pcWeb () {
    if (this.state.isMobileWeb !== true) {
      return [
        <div className='sky-container'></div>,
        <div className='sea-container'>
          <LoginForm onChange={this.onChange} ref='loginForm' onEmailChange={this.onEmailChange} onPasswordChange={this.onPasswordChange} />
          <input type='submit' value='Submit' onClick={this.login} />
        </div>
      ];
    }
  }
neaumusic
  • 10,027
  • 9
  • 55
  • 83
  • I don't know if you can disable React keys, it seems really important for handling state changes and dom rendering : https://facebook.github.io/react/docs/reconciliation.html#recursing-on-children – mfrachet Feb 01 '17 at 07:51

5 Answers5

32

There is a valid use case for not wanting to use keys, if you e.g. render strongly differing trees (think blog posts) or have a function that returns always the same array of items.

When you don’t provide an explicit key, React will use the index as key and emit a warning – however you would want to silence the warning, as using the index here is perfectly valid.

This doesn’t seem to be possible however, which has the sad consequence that you either have to make your code more complex for no reason (adding useless keys) or ignore the warnings (which means that valid warnings might be lost among the noise).

flying sheep
  • 8,475
  • 5
  • 56
  • 73
  • 14
    The is the real correct answer. Keys should be optional. React needs to relax a little with those intrusive warnings. – Nick Perkins Dec 01 '18 at 23:20
  • 1
    But they *are* optional, a warning is not an error and it doesn't stop your code from running in any way. – Francisco Presencia Sep 26 '19 at 13:32
  • 1
    React shouldn't be a linter like that, similar to the es-lint rule about multiple setStates potentially using out of date information, it's obvious if you know what you're doing – neaumusic Jan 13 '20 at 19:47
  • 2
    @FranciscoPresencia Even though they are optional, some warnings about key are more valid than others. Programmers should have a way to express that certain arrays are fixed, and others aren't (so no warnings are needed in the first place). – Khoa Vo Jun 09 '20 at 15:35
6

As of now, probably since 16.2.0 (November 28, 2017) you can wrap the elements inside <React.Fragment> instead of an array. The child components do not need keys although array of Fragments will still need keys.

RichN
  • 6,181
  • 3
  • 30
  • 38
2

you should not be passing the child element without key, React uses key for diff diff using virtual dom (it is lightweight javascript object). Never use key as Date.now() or Math.random(), because during render the react will see different key value and it will create new dom object.

Khalid Azam
  • 1,615
  • 19
  • 17
-1

A few years later, I will answer with newfound knowledge: the 'pointless wrapper' actually has semantic meaning to describe the collection and it's pretty normal to use a fragment eg <> ... </>, or a wrapper <div> to describe it, just not an array.

neaumusic
  • 10,027
  • 9
  • 55
  • 83
-3

Edit: Do not do this, as the comment by @Jason Miller suggests.


Whenever it is straightforward, it is best to simply use the indices of the array or some other ID. Sometimes, however, this is impractical, for example, when a complex layout is generated dynamically. A workaround for suppressing the warning is to generate a random number as a key using key={Math.random()}. There are around 15 safe digits in the returned float and thus >10^15 possible outcomes, which should be safe enough for the most applications.

Jakob Schödl
  • 380
  • 2
  • 16
  • 1
    Never do this. It will remount the tree rooted at the element with the random key on every render. Also don't use index as a key, it's worse than using no key: https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/no-array-index-key.md#prevent-usage-of-array-index-in-keys-reactno-array-index-key – Jason Miller Jul 20 '22 at 02:37