I have a React component and I want to toggle a css class when clicked.
So I have this:
export class myComponent extends React.Component {
construc
you can try a more declarative way. I changed your code to reflect this. You just need to remind that a component will refresh and call render in every state/props change. So, we can create the class of your element inside render method.
import React from 'react'
export default class myComponent extends React.Component {
constructor() {
super();
this.state = { clicked: false };
this.handleClick = this.handleClick.bind(this);
}
render() {
let btnClass = 'glyphicon'
if(this.state.clicked){
btnClass+=' active'
}
return (
);
}
handleClick() {
this.setState({
clicked: !this.state.clicked
})
}
}