I know that there are similar questions to this one, I've tried them both (1), (2) and didn't work for my case.
Having an array of objects on props:
this.props.items = [ { name: 'a', price: 10},
{ name: 'b', price: 20},
{ name: 'c', price: 10},
{ name: 'd', price: 30},
];
I want to create a list with all the price options and put that unique values on a dropdown so I'm doing this:
const priceOptions = [{ text: '', value: '' }];
const prices = [
...new Set(this.props.items.map(item => item.price)),
];
prices.forEach(price => {
if (price) {
priceOptions.push({
key: price,
text: price,
value: price,
});
}
this code throws the error:
Warning: Encountered two children with the same key,
ellipsisItem-NaN
. Keys should be unique so that components maintain their identity across updates. Non-unique keys may cause children to be duplicated and/or omitted — the behavior is unsupported and could change in a future version.
Don't know what does ellipsisItem
mean, that variable is not in the project.
I've read on the other questions' answers that it should be used the index instead of the element. So I changed it to:
const prices = [
...new Set(this.props.items.map(item => item.price)),
];
prices.forEach((price, index) => {
if (price) {
priceOptions.push({
key: index,
text: price,
value: price,
});
}
});
but it throws the same error.
It is possible that in the array of objects to have more objects with same price but on dropdown it should show only the unique values.
From 10, 20, 10, 30
to 10, 20, 30
UPDATE I'm adding also where it is rendered.
return (
<Layout>
...
<Dropdown
placeholder="Price"
onChange={this.searchHandlerPrice}
options={priceOptions}
/>
and the searchHandlerPrice:
searchHandlerPrice = (event, data) => {
const { getItems } = this.props;
geItems({
name: this.props.query.name,
price: data.value,
});
};
and < Dropdown >:
import React from 'react';
import { Dropdown } from 'semantic-ui-react';
import './Dropdown.styles.scss';
export default ({placeholder, options, onChange}) => {
return (
<Dropdown className="hello-dropdown"
placeholder={placeholder}
search
selection
options={options}
onChange={onChange}
clearable
/>
);
};