Function 1 Which returns JSON array.
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;
}
Function 2: Used Above function in below function
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>)
It is returning:
AND
I need HTML LIKE:
最佳答案
因为你想直接设置 HTML 你需要使用 dangerouslySetInnerHTML
像这样:
someHtml = '<div><strong>blablabla<strong><p>another blbla</p/></div>'
return(<div className="Container" dangerouslySetInnerHTML={{__html:
someHtml}}></div>)
但是,这不是您应该使用 jsx
和使用react的方式。更好的方法是使用 React.components
并渲染它们。
关于javascript - 如何在 react 中返回html文本变量并打印为html?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52665828/