abu abu
abu abu

Reputation: 7052

Each child in a list should have a unique "key" prop. in react

I have a Child component like below

import React, { Component } from 'react';

class InputText extends Component  {
  render = () => {    
    return (
      <div className="form-group">
        <label htmlFor={this.props.id}>{this.props.label}</label>
        <input type={this.props.type} value={this.props.value} placeholder={this.props.placeholder} name={this.props.name} id={this.props.id} className= {"form-control "+ this.props.class} required={this.props.extraValue}/>
      </div>
    )
  }
}

export default InputText

I am calling this in Parent component like below

<InputText class="" placeholder="Email here..." type="email" name="email" onChange={this.handleChange} extraValue={true} label={[<span className="text-danger">*</span>, " Email"]} />

I am getting error like below

Warning: Each child in a list should have a unique "key" prop.

Check the render method of `InputText`. It was passed a child from Register. See https://fb.me for more information.
    in span (at Register.js:182)
    in InputText (at Register.js:182)                                  

enter image description here

Upvotes: 0

Views: 2895

Answers (1)

kind user
kind user

Reputation: 41913

Every JSX element passed as an array has to have an unique id. Simply assign some key to the span element.

label={[<span key={1} className="text-danger">*</span>, " Email"]}
            // ^^^^ key

However I would suggest you to just pass it as two separate props, instead of passing down as an array.

label={'*'}
text={'Email'}

and inside your component:

<label htmlFor={this.props.id}>
   <span className='text-danger'>{this.props.label}</span>
   {this.props.text}
</label>

Upvotes: 2

Related Questions