javascript - 如何使用变量和函数调用将 html 字符串解析到 JSX 客户端

标签 javascript html reactjs ecmascript-6 jsx

html = "<p>Hello {this.getValue("a")}</p>"

基本上我想要实现的是渲染 html 后 getValue() 函数返回的值。

我尝试了 React HTML 解析器,但这显示了代码中编写的函数名称。

我还尝试了如下危险设置InnerHTML:

<div dangerouslySetInnerHTML={{ __html: html }} />

“dangerouslySetInnerHTML”的结果是相同的字符串

Hello {this.getValue("a")}

该字符串也来自 api 响应

示例:"<div class="test"><span>Overview</span></div><p><b>testing testing</b></p><p>{this.getValue('a')}, testing</p>"

最佳答案

使用模板文字 - 使用反引号 `` 和 ${}:

const getValue = arg => arg + arg;
const html = `<p>Hello ${getValue("a")}</p>`;
document.write(html);

关于javascript - 如何使用变量和函数调用将 html 字符串解析到 JSX 客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56354599/

相关文章:

reactjs - React New Context API - 跨多个文件访问现有上下文

javascript - 如何在jsp中保留mysql的last_insert_id()

javascript - 如何将选定的 div 置于所有其他 div 之上

javascript - 获取查找值的属性 - javascript/CRM 2011

javascript - JS/jQuery 更改悬停在 <tr> 上的图像

php - 处理从数据库动态填充的表单数据

javascript - 带有 webpack require() 的 Vue.js 动态图像 src 不工作

javascript - Django 表单不显示

javascript - 'ActionSheetIOS' 未从 'react-native-web/dist/index' 导出

reactjs - 无法修复 React-Table 列的宽度