Here my question is:
How can I change the state of my default set object using multiple select ?
Here let's say, I have states checkDevice
and checkCountry
, so in the below fiddle I did set all values to false
.
I want to set the values of only those properties to true
which are selected in the multi select dropdown.
For example: In multi select, if I just select US
and UK
then in my state is there a way to get something like:
checkCountry:
{
'US': true,
'UK': true,
'JP': false
}
class Box extends React.Component {
constructor() {
super();
this.state = {
checkDevice: {
'iPhone 4': false,
'iPhone 5': false,
'iPhone 6': false,
'iPhone 7': false,
'iPhone 8': false,
'iPhone X': false,
},
checkCountry: {
'US': false,
'UK': false,
'JP': false
}
}
}
componentDidMount() {
$(document).ready(function() {
$('#country').material_select();
});
$(document).ready(function() {
$('#device').material_select();
});
}
render() {
return (
<div className="card" style={{width:900,margin: 'auto'}}>
<form className="row">
<div className='col s6'>
<div className="input-field">
<select value="" multiple id="country">
<option value="" disabled selected>Choose your option</option>
<option value="1">US</option>
<option value="2">UK</option>
<option value="3">JP</option>
</select>
<label>Country</label>
</div>
</div>
<div className='col s6'>
<div className="input-field">
<select value="" multiple id="device">
<option value="" disabled selected>Choose your option</option>
<option value="1">iPhone 4</option>
<option value="2">iPhone 5</option>
<option value="3">iPhone 6</option>
<option value="4">iPhone 7</option>
<option value="5">iPhone 8</option>
<option value="6">iPhone X</option>
</select>
<label>Devices</label>
</div>
</div>
</form>
<div className="row">
<div className="col s6">
<pre>{JSON.stringify(this.state.checkCountry,null,2)}</pre>
</div>
<div className="col s6">
<pre>{JSON.stringify(this.state.checkDevice,null,2)}</pre>
</div>
</div>
</div>
);
};
}
ReactDOM.render( < Box / > , document.getElementById('root'));
<link href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.98.0/css/materialize.min.css" rel="stylesheet" />
<script src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.100.2/js/materialize.min.js"></script>
<div id='root'></div>
CODEPEN, this will give you more freedom to play with.
UPDATE:
Here in the official documentation of React, we can see how to change the complete state, but is there a way to change just the property of the existing value
For Example::
handleChange(event){
this.setState({checkCountry["event.target.value"] === true ? false: true});
}
But I don't think, I can get in this way. But I'm trying something like this.
Ans to questions that might help: Updating an object with setstate in react