OnClick event doesn't work for TreeView in React JS if it passed as props from another component

huangapple go评论71阅读模式
英文:

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(&quot;Item Clicked &gt;&gt; &quot; + e);
  }

  return (
    &lt;&gt;        
        &lt;TreeViewTracker   onClick={handleItemClick} /&gt;        
    &lt;/&gt;
  );
  }
}

export default TrackerStudent;

in TreeViewTracker.js Component , I wrote:

function TreeViewTracker(props) {

 &lt;TreeView &gt;
      ....	
	&lt;TreeItem  onClick={props.handleItemClick} /&gt;
      ....
 &lt;/TreeView&gt;
..
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
&lt;TreeViewTracker onClick={handleItemClick} /&gt;

<details>
<summary>英文:</summary>

You are passing `handleItemClick` function as a prop called `onClick` to the `TreeViewTracker` component:

```jsx
// TrackerStudent component
&lt;TreeViewTracker onClick={handleItemClick} /&gt;


</details>



huangapple
  • 本文由 发表于 2023年4月20日 10:10:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76060032.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定