javascript - 接受不同的 html 模板数据作为 reactjs 组件的选项
问题描述
在 reactjs 中,我如何使用模板 html 数据呈现组件?
class Options extends React.Component {
render() {
const othList = [];
[1, 2, 3, 4, 5].forEach((t) => {
othList.push(t);
});
return othList;
}
}
如何传递 html 数据并使用提供的 html 作为模板?
<Options><div></div></Options>
所以上面将呈现 5 个 div 主要是 1 个等。或者
<Options><td></td></Options>
将呈现 5 个 td
谢谢
解决方案
危险地设置内部 html
如果您将 html 作为字符串,则可以使用此方法在 react 中将其设置在组件中。
从文档
function MyComponent() {
return <div dangerouslySetInnerHTML={{__html: 'First · Second'}} />;
}
在您的代码中(假设 othList 是一个 html 模板数组)
class Options extends React.Component {
render() {
const othList = [];
[1, 2, 3, 4, 5].forEach((t) => {
othList.push(t);
});
let activeTemplateIndex = 0;
return <div dangerouslySetInnerHTML={{__html: othList[activeTemplateIndex]}} />;
}
}
推荐阅读
- android - 调试应用程序工作但发布应用程序不工作?
- javascript - 设置 vue.config.js 根据环境构建不同的页面
- python - 如何合并熊猫数据框但仅合并某些列
- python - 搜索特定值的嵌套字典列表
- html - 如何自动下载网页 HTML?拒绝访问
- python - Python 3,Pandas csv 行,在某些字段中嵌套了键/值。如何将其压平成一排宽
- delphi - 如何将结果编码为组合框中的选择?
- css - 在反应 mui 组件上使用 css codepen
- powershell - Powershell附加了文件夹中的所有文件
- javascript - 导入 fuse.js 库时出现打字稿错误