I'm trying to pass a parameter from a number of list items to differentiate them when clicked.
class SettingsGeneral extends React.Component {
constructor(props) {
super(props);
this.state = {
form_fields: ["Select option"],
selectedSetting: "general",
configSettings: {}
};
this.handleClick = this.handleClick.bind(this);
}
handleClick({ e }) {
const { param } = e.target.dataset;
console.log(param); // e.currentTarget.value would be equivalent
}
render() {
return (
<ul className="settings-list">
<li data-param="value" onClick={this.handleClick}></li>
</ul>
);
}
}
With the current code, I get an error:
TypeError: Cannot read property 'target' of undefined
Which means the value isn't getting sent to the handleClick function and I try log it in the console.
How do I go about doing this and what is the best way to do this? There are a lot of different answers online.
handleClick(e)