I'm trying to set the default value for my react-select
, but it is not working properly.
import Select from 'react-select';
I have the state for the selected properties
const [ selectedProps, setSelectedProps ] = useState([]);
In my useEffect, I set the value coming from the props
setSelectedProps(props.location.user.properties);
Right before rendering the component, I log the data to make sure that is correct
console.log(properties);
console.log(selectedProps);
Here is my select
<Select
defaultValue={selectedProps}
isMulti
name="properties"
options={properties}
className="basic-multi-select"
classNamePrefix="select"
onChange={handleChangeProperties}
/>
The defaultValue is not showing anything.
The log, at first both properties
and selectedProps
are empty. Then, the second log it has values:
Properties:
[
(3) […]
0: Object { value: 2618, label: "Prop 1" }
1: Object { value: 2309, label: "Prop 2" }
2: Object { value: 2192, label: "Prop 3" }
length: 3
<prototype>: Array []
selectedProps:
[
{
"value": 2618,
"label": "Prop 1"
},
{
"value": 2192,
"label": "Prop 3"
}
]
If I hard code he defaultValue it works, but not with the data coming from the variable.
useEffect
? can't you douseState(props.location.user.properties)
? – Mr. Aprops.location.user
is defined. Otherwise, I redirect the page. – myTest532 myTest532