I have a simple <Messages />
React component that shows the error/alert/success messages.
The code is simple as this:
render: function () {
return <div>{this.state.messages.map(function (message) {
return (<div className={message.type}>{message.text}</div>);
})}</div>;
}
The error Warning: Each child in an array or iterator should have a unique "key"
occurs because I don't have a key in the <div />
. But my message
doesn't have a field that I could call a "key". I thought to use message.text
as key, but it's a little weird...
My idea is simple: create a count
variable and use that as a key. As this:
render: function () {
var count = 0;
return <div>{this.state.messages.map(function (message) {
var keyValue = count++;
return (<div key={keyValue} className={message.type}>{message.type}</div>);
})}</div>;
}
Is there a problem in use this solution for this specific case?