I'm using a library called react-select
The dropdown/select works fine. However, I want to make the content dynamic which would come from an API
The API response is like this:
{
"id": 1,
"BIC": "AATCPHMMXXX",
"BRSTN": "010140015",
"NAME": "BANK CORPORATION"
},
{
"id": 5,
"BIC": "WEBPPHMMXXX",
"BRSTN": "010350025",
"NAME": "BANK OF THE WORLD"
}
ClientMaintenancePage.js
import Select from 'react-select';
...
const optionsBrstn = [
{ label: "BIC1 BRSTN1 NAME1", value: BRSTN1 },
{ label: "BIC2 BRSTN2 NAME2", value: BRSTN2 },
{ label: "BIC3 BRSTN3 NAME3", value: BRSTN3 }
...
];
<Select options={ optionsBrstn } />
I want it to appear in the dropdown with that pattern. Any suggestion would help, thanks.
ANSWER:
import Select from 'react-select';
...
const retrieveBanks = useCallback(() => {
ClientMaintenanceService.retrieveBanks()
.then((response) => {
setDataBanks(response.data);
}).catch((err) => {
console.log("ClientMaintenancePage - retrieveBanks catch >>> " + err)
})
});
useEffect(() => {
retrieveBanks();
}, []);
const newOptions = dataBanks.map(({BIC, BRSTN, NAME}) => ({
label: BIC + " " + BRSTN + " " + NAME ,
value: BRSTN
}))
return (
<Select
isSearchable="true"
options={ newOptions }
placeholder="Select BIC / BRSTN / Bank Name..."
/>
)
