1.应用程序请求CMS(内容管理系统)的页面内容。
2.CMS返回
"<div>Hi,<SpecialButton color="red">My Button</SpecialButton></div>"
3.应用程序使用提供的属性中的数据呈现相应的组件,消耗内容。
我无法想出如何以React方式执行第3步,请给予建议。
谢谢@Glenn Reyes,这里有一个展示问题的Sandbox。
import React from 'react';
import { render } from 'react-dom';
const SpecialButton = ({ children, color }) => (
<button style={{color}}>{children}</button>
);
const htmlFromCMS = `
<div>Hi,
<SpecialButton color="red">My Button</SpecialButton>
</div>`;
const App = () => (
<div dangerouslySetInnerHTML={{__html: htmlFromCMS}}>
</div>
);
// expect to be same as
// const App = () => (
// <div>Hi,
// <SpecialButton color="red">My Button</SpecialButton>
// </div>
// );
render(<App />, document.getElementById('root'));
这里有一个由Vuejs制作的实时演示。字符串"<div v-demo-widget></div>"
可以被视为Vuejs指令并呈现。 源代码。
<h4>Hello</h4><reactcomponenct attr1="foo"></mycomponenct>
这样的字符串后,如何让React知道<reactcomponenct attr1="foo"></mycomponenct>
是组件,并执行组件的代码。 - Sing