react: Don't warn on undefined key when using arrays

Using React.createElement('div', null, someArrayOfChildren) will always result in a warning when in development mode. I understand the reasoning when using JSX as you are more aware when the user may be doing something wrong. When using the javascript api, however, it seems odd because arrays are useful to work with and can be more performant than dealing with arguments.

Would it be possible to provide the ability to turn off this warning?

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 22 (7 by maintainers)

Commits related to this issue

Most upvoted comments

probably less likely when working with the javascript api

I wouldn’t be so sure, IMO maps would be just as common in plain JS code. It would be a shame to miss these cases just because somebody doesn’t use JSX.

On the other hand, you do have a way to suppress this warning if you’re just passing stuff around, and you found it yourself: use arguments.

You can spread the array for cleaner syntax (assuming you use an ES6 transpiler):

React.createElement('div', null, ...someArrayOfChildren); // no warning

I don’t think this has anything to do with JS / JSX differences. You can use arrays in both.

But if you don’t specify the keys, React won’t be able to efficiently tell if you moved an item or changed it so this may result in wasted time updating.

So, whether in JS or JSX, specifying key on each child will help React update DOM more efficiently. To specify key in plan JS, just pass it to children’s createElement as if it were a prop.

Oh yeah sorry I didn’t make it clear. I was referring specifically to this comment: https://github.com/facebook/react/issues/2816#issuecomment-344365259.

Ah, got it 🙂

You’d be able to do something like:

<React.Fragment key="foo">
  Hello
  my
  <h2>name</h2>,
  is
  <h2>{this.props.name}</h2>
</React.Fragment>

but I don’t think that solves the problem of wanting to use an array without keying.