07-19-2023, 04:38 PM
This is my senario :
1. Application request CMS(Content management system) for page contents.
2. CMS return `"<div>Hi,<SpecialButton color="red">My Button</SpecialButton></div>"`
3. Application consume the content, render corresponding component with data provided in attribute.
I can't figure out how to do **step 3** in React way, any advice is appreciated.
Thanks @Glenn Reyes, here's a [Sandbox](
<!-- language: lang-js -->
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'));
<!-- language: lang-js -->
[Here is a live demo](
1. Application request CMS(Content management system) for page contents.
2. CMS return `"<div>Hi,<SpecialButton color="red">My Button</SpecialButton></div>"`
3. Application consume the content, render corresponding component with data provided in attribute.
I can't figure out how to do **step 3** in React way, any advice is appreciated.
Thanks @Glenn Reyes, here's a [Sandbox](
[To see links please register here]
) to show the problem.<!-- language: lang-js -->
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'));
<!-- language: lang-js -->
[Here is a live demo](
[To see links please register here]
) made by Vuejs. String `"<div v-demo-widget></div>"` could be treat as Vuejs directive and rendered. [Source Code]([To see links please register here]
).