如何将字符串转换为 jsx? [英] How do I convert a string to jsx?
本文介绍了如何将字符串转换为 jsx?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我如何将一个字符串转换为jsx
?例如,如果我从 textarea
中引入一个字符串,我如何将其转换为 React
元素;
How would I take a string, and convert it to jsx
? For example, if I bring in a string from a textarea
, how could I convert it to a React
element;
var jsxString = document.getElementById('textarea').value;
我将使用什么过程在客户端转换它?可能吗?
What is the process I would use to convert this on the client? Is it possible?
推荐答案
可以考虑使用React属性dangerouslySetInnerHTML
:
You can consider using the React attribute dangerouslySetInnerHTML
:
class YourComponent{
render() {
someHtml = '<div><strong>blablabla<strong><p>another blbla</p/></div>'
return (
<div className="Container" dangerouslySetInnerHTML={{__html: someHtml}}></div>
)
}
}
这篇关于如何将字符串转换为 jsx?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文