javascript - 如何在 React js 中的模板文字中调用 onclick 函数

标签 javascript reactjs template-literals

请帮助我在 React JS 的模板文字中编写点击函数。通过单击模板文字中的按钮,我需要调用 React js 中的另一个函数。

  addPopupToLayer = (surface, layer) => {
  const { surface_type: { name, color } } = surface;
  const customPopup = `
   <div class=${styles.tooltipHeader}>
      <h4>${surface.name}</h4>
      <button onclick=${()=> this.viewDetails(surface)}>View</button>
    </div>   
    <p>${name}</p>
  `;
  const customOptions = { className: styles.tooltip, width: '200' };
  const customlayer = layer.bindPopup(customPopup ,customOptions );

  customlayer.setStyle({
     fillColor: color,
     color,
     opacity: 1,
     fillOpacity: 0.48
  });
  return customlayer;
};


 viewDetails = (surface) => {
   console.log("View Details is Called")
  }

最佳答案

调用函数时去掉$符号即可生效

<button onclick={()=> ViewDetails(surface)}>View</button>

关于javascript - 如何在 React js 中的模板文字中调用 onclick 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59678946/

相关文章:

jquery - 错误: Path does not match the corresponding path on disk

javascript - 在 ES2015 中,如何以正确的顺序重建带标签的模板文字?

JavaScript ES6 - 带模板文字的 console.log

javascript - chop 具有多个字符的字符串而不 chop 单词

javascript - 如何用数组值替换字符串中的多个占位符?

ios - React Native 选择器值在选择时重置

javascript - 如何从当前 URL 获取路由名称

javascript - 使用字符串中数组的每个元素组成标准化的句子

javascript - 如何浏览 div 中的每个链接并根据条件更新文本

javascript - 访问 JQuery data-json 元素