React-select: Change name properties from options data

Created on 6 Sep 2018  路  3Comments  路  Source: JedWatson/react-select

Hi, I get an array from API, but it does not have properties such as 'label' or 'value'. How can I choose which property is belongs to label and value, without changing the array?

Example:

I have this:
options = [{ name: "Black", id: 1 }, { name: "White", id: 2 } ];

Examples from react-select:
options = [{ label: "Black", value: 1 }, { label: "White", value: 2 } ];

Regards

Most helpful comment

I had a similar object setup and solved it with these props on the Select component:

getOptionLabel={(option) => option.name}
getOptionValue={(option) => option.id}

All 3 comments

options = options.map(option => {label: option.name, value: option.id});

I had a similar object setup and solved it with these props on the Select component:

getOptionLabel={(option) => option.name}
getOptionValue={(option) => option.id}

Works fine. Thanks so much @jdt3969 !

Was this page helpful?
0 / 5 - 0 ratings