英文:
OnClick event doesn't work for TreeView in React JS if it passed as props from another component
问题
I have two components:
1- TrackerStudent.js
2- TreeViewTracker.js
I used props to provide an event onClick to TreeViewTracker component:
--
function TrackerStudent() {
function handleItemClick(e) {
console.log("Item Clicked >> " + e);
}
return (
<>
<TreeViewTracker onClick={handleItemClick} />
</>
);
}
export default TrackerStudent;
In TreeViewTracker.js Component, I wrote:
function TreeViewTracker(props) {
<TreeView >
....
<TreeItem onClick={props.handleItemClick} />
....
</TreeView>
export default TreeViewTracker;
But the onClick event wasn't triggered.
I tried
`onClick={handleItemClick}`
英文:
I have two components:
1- TrackerStudent.js
2- TreeViewTracker.js
I used props to provide an event onClick to TreeViewTracker component :
function TrackerStudent() {
function handleItemClick(e) {
console.log("Item Clicked >> " + e);
}
return (
<>
<TreeViewTracker onClick={handleItemClick} />
</>
);
}
}
export default TrackerStudent;
in TreeViewTracker.js Component , I wrote:
function TreeViewTracker(props) {
<TreeView >
....
<TreeItem onClick={props.handleItemClick} />
....
</TreeView>
..
export default TreeViewTracker;
But the onClick event wasn't triggered.
I tried
`onClick={handleItemClick}`
答案1
得分: 1
You are passing handleItemClick
function as a prop called onClick
to the TreeViewTracker
component:
// TrackerStudent component
<TreeViewTracker onClick={handleItemClick} />
<details>
<summary>英文:</summary>
You are passing `handleItemClick` function as a prop called `onClick` to the `TreeViewTracker` component:
```jsx
// TrackerStudent component
<TreeViewTracker onClick={handleItemClick} />
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论