I have tried interchanging the words key and id and still its showing the error of 'Each child in a list should have a unique "key" prop.
Check the render method of Counters
.
in Counter (at Counters.jsx:17)
in Counters (at src/index.js:10)'
import React, { Component } from "react"; // this is counters.jsx file.
import Counter from "./Counter";
class Counters extends Component {
state = {
counters: [
{ id: 21, value: 4 },
{ id: 22, value: 2 },
{ id: 33, value: 3 },
{ id: 44, value: 0 }
]
};
render() {
return (
<div>
{this.state.counters.map(counter => (
<Counter key={Counter.id } value={Counter.value} selected={true} />
))}
</div>
);
}
}
export default Counters;
Counter. import React, { Component } from "react"; //this is counter.jsx file.
class Counter extends Component {
state = {
count: 0
};
handleIncrement = () => {
this.setState({ count: this.state.count + 1 });
};
render() {
console.log("props", this.props);
return (
<div>
<span className={this.getBadgeClasses()}>{this.formatCount()}</span>
<button
onClick={this.handleIncrement}
className="btn btn-secondary btn-sm"
>
Increment
</button>
</div>
);
}
getBadgeClasses() {
let classes = "badge m-2 badge-";
classes += this.state.count === 0 ? "warning" : "primary";
return classes;
}
formatCount() {
const { count } = this.state;
return count === 0 ? "Zero" : count;
}
}
export default Counter;
Counter
instead ofcounter
. – Antoni