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!
BEFORE (Working):
const Card = ({name, email}) => {
return (
<div>
<h2 className='f5'>{name}</h2>
<p className='f6'>{email}</p>
</div>
</div>
);
}
AFTER (Not Working):
class Card extends Component {
constructor({props, name}) {
super(props);
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;