Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
321 views
in Technique[技术] by (71.8m points)

javascript - 插入带有React变量语句(JSX)的HTML(Insert HTML with React Variable Statements (JSX))

I am building something with React where I need to insert HTML with React Variables in JSX.(我正在用React构建一些东西,我需要在JSX中插入带有React变量的HTML。)

Is there a way to have a variable like so:(有没有办法像这样一个变量:) var thisIsMyCopy = '<p>copy copy copy <strong>strong copy</strong></p>'; and to insert it into react like so, and have it work?(并像这样将其插入反应中并起作用?) render: function() { return ( <div className="content">{thisIsMyCopy}</div> ); } and have it insert the HTML as expected?(并按预期插入HTML吗?) I haven't seen or heard anything about a react function that could do this inline, or a method of parsing things that would allow this to work.(我还没有看到或听到任何有关可以完成此内联的react函数的信息,也没有听说过任何使之能够工作的解析方法。)   ask by Kyle Hotchkiss translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use dangerouslySetInnerHTML , eg(您可以使用dragonallySetInnerHTML ,例如)

render: function() { return ( <div className="content" dangerouslySetInnerHTML={{__html: thisIsMyCopy}}></div> ); }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...