I can not figure out if I am missing something small here?
Just trying to get a grasp on how state works with React.
Just creating a small check box that changes text on check/uncheck.
import React from 'react';
export default class Basic extends React.Component {
constructor(props) {
super(props);
this.state = {
checked: true
};
}
handleCheck() {
this.setState = ({
checked: !this.state.checked
});
}
render() {
var msg;
if (this.state.checked) {
msg = 'checked'
} else {
msg = 'unchecked'
}
return (
<div>
<input type="checkbox" onChange={this.handleCheck} defaultChecked={this.state.checked} />
<h3>Checkbox is {msg}</h3>
</div>
);
}
}
bindthe onChange method, use this:onChange={this.handleCheck.bind(this)}or bind the method in the constructor. - Mayank Shuklathis.setState = ({it should bethis.setState({- Mayank Shukla