I have two components
which displaying
each element of const elementObjects = [{id: 1, Element: "Orange", Weight: 55}, {id:2, Element: "Banana", Mass: 20}];
in an unorderd list
I want to log the value of a list item to the console if clicked
return <li onClick={(e)=> console.log(e.target.value)}>{props.value}</li>;
when clicked the eventHandler return 0 instead of Orange
how can I get the desired behavior ?
function ListItem(props) {
// --> displays the data / is reusable
return <li onClick={(e)=> console.log(e.target.value)}>{props.value}</li>;
}
function ChooseElements() {
const listItems = elementObjects.map((object) =>
<ListItem key={object.id.toString()} value={object.Element} />
);
return (
<ul>
{listItems}
</ul>
);
}
ReactDOM.render(
<ChooseElements />,
document.getElementById('root')
);
props.value(the value of thevalueprop) instead ofe.target.value(the value of thevalueattribute of theliHTML element, which is unlikely to be anything interesting, since you haven't set it to anything interesting.