Warning: Each child in an array or iterator should have a unique "key" prop

1

I can not find where this warning is playing or why, any ideas?

import React from 'react';
import logo from '../../../raw-assets/svg/logo.svg';
import line from '../../../raw-assets/svg/line.svg';
import 'whatwg-fetch'; 
import {backendURL, backendPort} from '../../components/connect.js';
if(process.env.NODE_ENV == 'production'){
var backend = "";
}else{
  var backend = backendURL +':'+ backendPort;
}
import isMobile from 'ismobilejs';
var Menu = require('react-burger-menu').slide;

class ResponsiveHeader extends React.Component {

	constructor() {
    super();
    this.state = {
      categories: [],
      mobile: 'span',
    }
  }

  componentDidMount() {
    fetch(backend+'/api/categories')
    .then(result => result.json())
    .then(categories => {this.setState({categories});
  });
  }

  selectKey(event) {  
    this.setState({ exampleState: event.target.value });
  }

  render() {
    if(!isMobile.any){
      var control = 'logosection';
    }else{
      var control = 'logosection-mobile';
    }
    
     return ( 
       
      <div className="header">
        <div className={control}>
        <div className="logo" dangerouslySetInnerHTML={{ __html: xxx }} />
        <div className="line" dangerouslySetInnerHTML={{ __html: line }} />
        <div className="labs">LABS</div>
      </div>

{isMobile.any &&
       
        <Menu right={1}>
    <div className="check-sidebar">
 <ul>
       <div className="languages-header">
      <p>CODE LANGUAGES:</p>
      </div>
      {
      this.state.categories.map(c =>
<li className="checkbox">
            <input id={c.id} type="checkbox" onChange={this.onChange.bind(this)} value={c.id}/>
            <label htmlFor={c.id}></label>
            <tag>{c.name}</tag>
</li>
          )
        }
</ul>
</div>


        </Menu>

      }

   
{!isMobile.any &&
      <div className="check">
      <div className="languages-header">
      CODE LANGUAGES:
      </div>
      {
      this.state.categories.map(c =>
<span className="checkbox">
            <input id={c.id} type="checkbox" onChange={this.onChange.bind(this)} value={c.id}/>
            <label htmlFor={c.id}></label>
            <tag>{c.name}</tag>
</span>
          )
        }
      </div>
       }
      </div>

    );
   }
   onChange (e) {
    this.context.applyFilters(parseInt(e.target.value));
  }

}

ResponsiveHeader.contextTypes = {
  applyFilters: React.PropTypes.func
};


export default ResponsiveHeader;

    
asked by Santiago D'Antuoni 23.03.2017 в 19:18
source

2 answers

1

The importance of this, is that we remember that React uses a virtual DOM to then make a diff with the real DOM, and know which element was updated.

What does all this have to do with the Key ??? If you do not put a key at the time of generating the list and make a change to one of the children the virtual DOM will understand that you did an update to all and re-render, this is why you need to place a key, to help and save time of performance.

As Alejandro says, just placing a key = {{algunvalornorepetido}} is solved.

    
answered by 23.03.2017 / 20:16
source
0

This error occurs when you are creating a list of elements in react and do not add a different "key" for each of them, as I recall.

I think that ResponsiveHeader is adding elements within itself, it has months that I do not touch react, but if you are going to create child components, make sure that each child has a "key" similar to

key = {{algunvalornorepetido}}

For each of the child elements that you create within that Component

    
answered by 23.03.2017 в 19:43