I have the following code as part of my React component:
When setting the value for your select component, you will have to convert null
to ''
; and when receiving the value from your component, you will have to convert ''
to null
. A simple example:
class Example extends React.Component {
constructor(props) {
super(props);
this.state = { selected: null };
}
render() {
return
this.setState({ selected: null })} value='Reset' />
}
}
This works assuming that your ids are always truthy: e.target.value || null
will convert the selected empty string to null
; and this.state.selected || ''
will convert your null
state to an empty string. If your ids can be falsey (for example the number 0
), you will need a more robust conversion.
See Fiddle here.