1

I've got the following error:

warning.js?0260:45Warning: Each child in an array or iterator should have a unique "key" prop. Check the render method of Tabs.

  render() {

    const { tabs } = this.props;
    const { active } = this.state;
    return (
      <div>
        <ButtonToolbar>
          {tabs.map(listValue =>
            <Button  onClick={() => this.handleClick(listValue)}>
              {listValue}
            </Button>
          )}
        </ButtonToolbar>

        // Display the chosen tab
        {this.showTab(active)}



      </div>
    );

Where would I add the id property in this case. I've tried:

<ButtonToolbar>
              {tabs.map(listValue =>
                <Button key={listValue.id}  onClick={() => this.handleClick(listValue)}>
                  {listValue}
                </Button>
              )}
            </ButtonToolbar>

But it does not get rid of the error. Please advise

2
  • if you console.log(listValue.id) are they all unique? Commented Sep 30, 2016 at 23:04
  • You could also do tabs.map((listValue, index) => <Button key={listValue.id + index} onClick={() => this.handleClick(listValue)}> {listValue} </Button> )} Commented Sep 30, 2016 at 23:09

1 Answer 1

2

You can use map's iterator for this:

<ButtonToolbar>
  {tabs.map((listValue, key) =>
    <Button              ^--- here
      key={key}   <--- and here.
      onClick={() => this.handleClick(listValue)}
    >
      {listValue}
    </Button>)
  }
</ButtonToolbar>

Reference

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.