Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
183
rated 0 times [  189] [ 6]  / answers: 1 / hits: 35103  / 5 Years ago, wed, october 30, 2019, 12:00:00

Trying to use Material UI checkbox. Pretty simple one might think? Well the checkbox doesn't toggle. Turns out the onChange event is not fired even internally to the component (I put logs in the node_modules package).



      <Checkbox
checked={this.state.isTrue}
onChange={e => {
console.log(e.target.checked);
this.setState({isTrue: e.target.checked});
}} />


Pretty simple, right? But the console.log never fires. I can hack around it by putting an onClick event handler on the component and toggling the state manually, but that is silly. Anyone have a clue?



The API is at https://material-ui.com/api/checkbox/#checkbox. Not rocket science.


More From » reactjs

 Answers
7

The issue might come from the structure of your component as provided code is perfectly fine, here is a working exemple you can try on codesandbox.io.



Compare with your code and try to find differences, but isolating a specific element might be a good way to realise the issue might come from somewhere else.



import React, { Component } from react;
import { render } from react-dom;
import Checkbox from material-ui/Checkbox;

class App extends Component {
constructor() {
super();
this.state = {
isTrue: false
};
}
render() {
return (
<div>
<Checkbox
checked={this.state.isTrue}
onChange={e => {
console.log(e.target.checked);
this.setState({ isTrue: e.target.checked });
}}
/>
</div>
);
}
}

render(<App />, document.getElementById(root));


[#51534] Friday, October 18, 2019, 5 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
mercedez

Total Points: 525
Total Questions: 103
Total Answers: 102

Location: Trinidad and Tobago
Member since Fri, Mar 24, 2023
1 Year ago
mercedez questions
;