Lots of problems here.
1) You are immediately invoking your function in your onClick. This will be evaluated - you need to make sure what you pass to onClick is a function, not the invocation of a function.
2) changeClass name won't be bound to the instance of the class this is in. When you do eventually get to invoke it from clicking the button, this
won't be what you expect, so setState
is unlikely to work. You can use an arrow function like syntax to solve this if you're using create-react-app.
3) You can't change a className of a JSX element like that - you are far better off inserting it with Javascript for the transpiler to deal with.
Here's a possible solution:
state = {
className: "changeMe"
};
changeClassName = (newName) => {
this.setState({
className: newName
});
};
render() {
const { className } = this.state;
return (
<div className={className}>
<button onClick={() => this.changeClassName('whatever you want')}>
Use whatever variable in the arg above you like. Notice how I have passed in anonymous func, which will invoke my method for me.
</button>
</div>
);
}