Here I want to set state the value in setstate as integer in the in my reactjs component. I am unable to do so since the value is not set as integer so in radio button its does not comes as checked.
constructor(props) {
super(props);
this.state = {
frequency: 1,
}
handleSelectChange(event) {
this.setState({
frequency: event.target.value
})
};
<div className = "custom-control custom-radio custom-control-inline"
onChange = {
this.handleSelectChange.bind(this)
} >
<
input type = "radio"
id = "oneTime"
value = "0"
name = "type"
onChange = {
event => this.handleSelectChange(event)
}
checked = {
this.state.frequency === "0"
}
className = "custom-control-input" / >
<
label className = "custom-control-label"
htmlFor = "oneTime" > {
Labels.PRODUCT_SUBSCRIPTION.ONETIME
} <
/label> < /
div > <
div className = "custom-control custom-radio custom-control-inline" >
<
input type = "radio"
id = "continuous"
value = "1"
name = "type"
checked = {
this.state.frequency === "1"
}
onChange = {
event => this.handleSelectChange(event)
}
className = "custom-control-input" / >
<
label className = "custom-control-label"
htmlFor = "continuous" > {
Labels.PRODUCT_SUBSCRIPTION.CONTINUOUS
}
</label>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
I know we can set this by using defaultValue. But then it becomes uncontrolled which is not encouraged.