I\'m having trouble to update the checkbox state after it\'s assigned with default value checked="checked" in React.
var rCheck = React
There are a few ways to accomplish this, here's a few:
Written using State Hooks:
function Checkbox() {
const [checked, setChecked] = React.useState(true);
return (
);
}
ReactDOM.render(
,
document.getElementById('checkbox'),
);
Here is a live demo on JSBin.
Written using Components:
class Checkbox extends React.Component {
constructor(props) {
super(props);
this.state = {
isChecked: true,
};
}
toggleChange = () => {
this.setState({
isChecked: !this.state.isChecked,
});
}
render() {
return (
);
}
}
ReactDOM.render(
,
document.getElementById('checkbox'),
);
Here is a live demo on JSBin.