I have his code where i try to pass a variable to the handleclick fcn and set state to that variable:
class MyComponent extends React.Component {
constructor(props) {
super(props);
this.state = {
name: 'Initial State'
};
this.handleClick = this.handleClick.bind(this);
}
handleClick(temp) {
this.setState({
name:temp
});
}
render() {
return (
<div>
<button onClick={this.handleClick('name')}>Click Me</button>
<h1>{this.state.name}</h1>
</div>
);
}
};
It doesn't work, can someone explain how to pass a variable and set state to it, if it's possible??