I m totally new to React. I am trying to pass a variable 'name' to my class, but it is not getting assigned. These names comes from another file. I cannot see names of people on my buttons. Before my Card was just a simple function and it was working fine. But after converting it into a class, name is not working. Pls help!
class Card extends Component {
constructor({props, name}) {
super(props, name)
this.state = {
isToggleOn: true,
name: this.name
};
this.handleClick = this.handleClick.bind(this);
}
handleClick() {
this.setState(prevState => ({
isToggleOn: !prevState.isToggleOn
}));
}
render() {
var { name } = this.state;
return (
<button onClick={this.handleClick}>
{this.state.isToggleOn ? name : 'OFF'}
</button>
);
}
}
ReactDOM.render(
<Card />,
document.getElementById('root')
);
export default Card;
Aucun commentaire:
Enregistrer un commentaire