javascript - 如何在 react 中返回html文本变量并打印为html?

标签 javascript html reactjs redux react-redux

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:

enter image description here

AND

enter image description here

I need HTML LIKE:

enter image description here

最佳答案

因为你想直接设置 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/

相关文章:

javascript - 如何创建一个常量、 Action 创建器和 reducer 工厂?

html - CSS - 在不同的行上维护标签和输入

javascript - 如何在 html block 内循环 javascript 变量?

javascript - 尝试将 React 连接到 redux

javascript - 清除不活动时的表单字段

javascript - infusion.com 是如何扭曲元素的? CSS 文件似乎没有倾斜,菜单也倾斜

javascript - 无法读取 null 的属性 addEventListener

javascript - 等待几秒钟,然后再运行下一步 Protractor

javascript - 使用 this for 迭代器而不是 for 循环

javascript - 是通过 Prop 将对象传递给子 react 组件,克隆原始对象还是通过引用传递?