I want to change the className
of an div based on whether it’s clicked or not by the user.
export default class Dimension extends Component {
constructor(props){
super(props)
this.state ={
click : false
}
}
render() {
return (
<div className='scroll-container'>
<div className={this.state.click ? 'circle-one' : "circle"} id='circle-one' onClick={() => {
this.state.click = !this.state.click;
console.log(this.state.click)
}}></div>
</div>
)
}
}
I have tried to use constructor, arrow function and even using bind and they always said that they cant read property of null
.
3
Answers
Change how you mutate your state
Component
Also unless you are maintaining some legacy project I would highly recommend upgrading your version of react and moving away from class components
You need to call
this.setState
to change the component’s state. If you try to setthis.state.click
directly, the component will not update.I would also make the event handler callback a method of the component, rather than an in-line arrow/lambda.