[英]How to call a onclick function inside a Template literal in React js
請幫助我在 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>
const customPopup =
`<div class=${styles.tooltipHeader}>
<h4>${surface.name}</h4>
<button onClick={()=> ViewDetails(${surface})}>View</button>
</div>
<p>${name}</p>`;
如果您使用的是表面變量,那么它需要在模板文字中使用 $ 符號來指示表面是一個變量而不是結果字符串的一部分。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.