javascript - 如何在反应中返回html文本变量并打印为html?
问题描述
函数 1 返回 JSON 数组。
function allPlans()
{
var all_plans = {
'Stock Card Views' : {
'free':{status:true,plantext:"5 Per Month"},
'premium':{status:true,plantext:"Unlimited"},
'vip':{status:true,plantext:"5 Per Month"}
},
'Portfolio Creation' : {
'free':{status:true,plantext:"Unlimited"},
'premium':{status:true,plantext:"Unlimited"},
'vip':{status:true,plantext:"Unlimited"}
},
'Transactions In A Portfolio' : {
'free':{status:true,plantext:"Unlimited"},
'premium':{status:true,plantext:"Unlimited"},
'vip':{status:true,plantext:"Unlimited"}
},
'Advance Filter':{
'free':{status:true,plantext:"-"},
'premium':{status:true,plantext:"Full Access"},
'vip':{status:true,plantext:"Full Access"}
},
'Stock Card Requests' : {
'free':{status:true,plantext:"-"},
'premium':{status:true,plantext:"3 Per Month"},
'vip':{status:true,plantext:"3 Per Month"}
},
'Premium Posrfolio Access' : {
'free':{status:true,plantext:"-"},
'premium':{status:true,plantext:"-"},
'vip':{status:true,plantext:"1 Portfolio"}
},
'Investment Pick' : {
'free':{status:true,plantext:"-"},
'premium':{status:true,plantext:"-"},
'vip':{status:true,plantext:"1 Per Month"}
},
}
return all_plans;
}
功能2:在以下功能中使用以上功能
function renderPlansArray()
{
var all_plans = allPlans();
var rowclass = true;
var htmltext = "";
for(var PlanName in all_plans)
{
console.log(plan_item_gray);
if (props.planname == 'free')
{
if(rowclass == true)
{
rowclass = false;
htmltext += <div className={plan_item_gray}>all_plans[PlanName]['free']['plantext']</div>;
}
else
{
rowclass = true;
htmltext += <div className={plan_item}>all_plans[PlanName]['free']['plantext']</div>;
}
}
}
return(htmltext);
}
在上面的函数中,我在htmltext
变量中生成了一个 HTML 文本并返回。但是当我像上面一样返回时,这将返回 [object Object],当我转换为字符串时,它将按原样打印 HTML 文本。
我想将此变量作为 HTML 返回。
我在另一个函数中调用了上面的函数来呈现如下所示的 HTML。
return (<div className={plan_column}>
{renderPlansArray()}
</div>)
它正在返回:
和
我需要像这样的 HTML:
解决方案
由于您想直接设置 HTML,因此您需要使用以下内容dangerouslySetInnerHTML
:
someHtml = '<div><strong>blablabla<strong><p>another blbla</p/></div>'
return(<div className="Container" dangerouslySetInnerHTML={{__html:
someHtml}}></div>)
但是,这不是您应该使用jsx
和反应的方式。更好的方法是使用React.components
和渲染它们。
推荐阅读
- c# - 在 C# 中使用 Linq 单击按钮时,Json 不会转到下一个或上一个文本
- php - 将类似 NoSQL 的语法转换为 MySQL 语法
- java - 每个盒子包含多个信息的数组
- java - 如何在另一个类中获取一个类的字段名称?
- django - DRF 文件上传,如何解决文件丢失错误?
- bash - 展开条件语句中的最后一个命令
- routes - TYPO3 v10 上的自定义路由方面不起作用
- ruby-on-rails - 提交表单后如何添加附加条件到ransack
- javascript - 如何在 React Native 中在后台发送电子邮件
- mysql - 使用 SQL 计算一周内完成的票数?(每周自动运行)