admin管理员组文章数量:1418437
I have two ponents
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')
);
I have two ponents
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')
);
Share
Improve this question
asked May 2, 2021 at 18:06
seemanseeman
1151 silver badge15 bronze badges
1
-
Try
props.value
(the value of thevalue
prop) instead ofe.target.value
(the value of thevalue
attribute of theli
HTML element, which is unlikely to be anything interesting, since you haven't set it to anything interesting. – Heretic Monkey Commented May 2, 2021 at 18:10
5 Answers
Reset to default 3You don't need e.target
. Your value is ing from your props. Your ListItem
should look like this to log the value once clicked:
function ListItem(props) {
return <li onClick={() => console.log(props.value)}>{props.value}</li>;
}
here you go. use props.value for onClick
function ListItem(props) {
return <li onClick={()=> console.log(props.value)}>{props.value}</li>;
}
Can you use value from props instead of event target?
Something like this:
function ListItem(props) {
return <li onClick={() => console.log(props.value)}>{props.value}</li>;
}
?
li elements don't specifically have e.target.value So you'll have to console.log props.value
function ListItem(props) {
// --> displays the data / is reusable
return <li onClick={(e)=> console.log(props.value)}>{props.value}</li>;
}
You could simply pass the props.value to the onClick event handler and there won't be a need for e.target.value.
本文标签: javascriptget value of list item with click event in ReactStack Overflow
版权声明:本文标题:javascript - get value of list item with click event in React - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745279274a2651345.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论