This has been asked quite a few times, so sorry, but I can't work this out. I hae read the docs, but I couldn't find anything that worked, so I obvioulsy don't understand what's happening here.
class DivisionExtraDetails extends Component {
constructor(props) {
super(props);
this.state = {
error: false,
loading: true,
removing: null,
saving: false,
geofence: false,
startTimeOfDay: ''
};
}
componentDidMount() {
const { division } = this.props;
Axios.get(`${API_URL}assetgroups/${division.id}`)
.then(r => {
this.setState({
loading: false,
geofence: r.data.geofence_assign,
startTimeOfDay: r.data.day_start
});
})
.catch(err => {
if (!Axios.isCancel(err)) {
this.setState({
error: true
});
}
});
}
render() {
const { error, loading, geofence, saving, startTimeOfDay } = this.state;
const { assignText, division } = this.props;
const geoFenceOptions = [
{value: 1, label: 'YES'},
{value: 0, label: 'NO'},
{value: null, label: 'Not yet set'},
];
return (
<React.Fragment>
<div className="col-5">
<span>Assign a GeoFence (Yes/No)</span>
<Select
selectedValue={geofence}
options={geoFenceOptions}
className="basic-multi-select"
classNamePrefix="select"
onChange={this.handleChange}
/>
</div>
</React.Fragment>
);
}
}
I've also tried:
defaultValue={geofence}
selectedValue={geofence}
value={geofence}
And I've also tried the variable as:
{this.state.geofence}
I can see the call to the db is correctly populating the state if I view it in dev tools. But I can't work it out. If anyone can help with this seemingly simple task, that would be great. Thanks.
geofence?valuein react select as boolean or string so that's why it is not working. Try to pass value as object like{value: false, label: 'N0'}