簡體   English   中英

無法將點擊事件附加到反應組件內的錨標記

[英]Not able to attach click event to an Anchor tag inside a react component

下面是我的代碼 ComponentA.js

組件內部的 return 語句

 return (
       ......

        <a id="MytoolTip" ......  

       <ComponentB
          content={
            `
            <div class="share_cart_tt">
                  <a 
                  data-automation-id="..."
                  class="callFunction" 
                   > Invite </a>
            </div>
           `
          }
          target={'MytoolTip'}
          closeButton
       />

);

ComponentB.js(這是一個工具提示,當用戶點擊錨標簽 MytoolTip 時會顯示)

.....

class ComponentB extends Component {
  launchModal() {
    console.log("hey its fine");
  }
     ...
      renderContent = () => {
          document.getElementsByClassName('callFunction').
          addEventListener('click', this.launchModal);
      **I am trying to bind click event here but its not working out**
     }

}

我是初學者反應,我嘗試了不同的方法來綁定點擊事件,但沒有任何效果..需要幫助。 當用戶點擊工具提示內的錨標簽時,類 .callFunction console.log 應該被打印出來。

請注意,我正在嘗試將 onClick 事件添加到錨標記,這只是 ComponentA 中的靜態內容,並且將通過在 ComponentB 中的 prop.content 中獲取靜態內容來創建工具提示

React 組件具有合成事件偵聽器 您需要做的就是將onClick屬性添加到元素。 所以你的看起來像這樣:

return (
       ......

        <a id="MytoolTip" ......  

       <ComponentB
          content={
            `
            <div class="share_cart_tt">
                  <a 
                  data-automation-id="..."
                  onClick={FunctionToBeCalledWhenClicked}
                  class="callFunction" 
                   > Invite </a>
            </div>
           `
          }
          target={'MytoolTip'}
          closeButton
       />

);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM