React render components from string

Alisson

Is it possible to dynamically render a React component from a string?

Basically I have pages' content coming from the database and I want to have React componentes within the content. An example of what I'm trying to achieve:

var html_string = '<i>React Component Rendered</i>: <Hello name="World" />';

var Hello = React.createClass({
  render: function() {
    return <strong>Hello {this.props.name}</strong>;
  }
});

function createMarkup() { return {__html: html_string}; };

var App = React.createClass({
  render: function() {
    return <div dangerouslySetInnerHTML={createMarkup()} />;
  }
});

ReactDOM.render(
  <App/>,
  document.getElementById('container')
);

But it just renders the HTML without evaluating <Hello/>:

<div><i>React Component Rendered</i>: <hello name="World"></hello></div>

I'm trying to get:

<div><i>React Component Rendered</i>: <strong>Hello World</strong></div>

This example on JSfiddle: https://jsfiddle.net/26czprne/

mason505

As thangngoc89 as said there is no way to do this. At least no simple way.

JSX is transpiled into javascript, it only syntactically looks like xml.

this:

var x = <div> <i>React Component Rendered</i>: <Hello name="World" /> </div>;

is mapped to:

var x = React.createElement(
  "div",
  null,
  " ",
  React.createElement(
    "i",
    null,
    "React Component Rendered"
  ),

  ": ",
  React.createElement(Hello, { name: "World" }),
);

A better strategy would probably be to parse the database elements and then dynamically render the result of the db query using react (this also promotes looser coupling).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

React with Typescript: Render components from `map` function

From Dev

How to render a react component from a string name

From Dev

Render multiple components in React Router

From Dev

Spread an array of components in render in React

From Dev

How to progressively render react components?

From Dev

Render a string which contains components

From Dev

Is there a way to render multiple React components in the React.render() function?

From Dev

React js render html string returned from server

From Dev

Generate React components in PHP and then render in client

From Java

how to render react components by using map and join

From Java

Rendering React components with promises inside the render method

From Dev

How to dynamically load & render react components?

From Dev

react.js render components at different locations

From Dev

Render two components adjacent to each other in React

From Dev

Render multiple React components into a single DOM element

From Dev

React native button click render different components

From Dev

Re-render all components in React and Redux

From Dev

React data components table does not render HTML

From Dev

Use Tooltip.js to render React Components

From Dev

How to render components using function in React Native

From Dev

Render components inside other components with react.js

From Dev

How to test react components that render other components with props?

From Dev

Render JSF components directly from Javascript

From Dev

Render image from props in react,

From Dev

Express Render Template from String

From Dev

Render pug template from string?

From Dev

how to render child components in react.js recursively

From Java

React Router doesn't render components after build to ftp server

From Dev

React: Abstraction for components. Composition vs Render props

Related Related

  1. 1

    React with Typescript: Render components from `map` function

  2. 2

    How to render a react component from a string name

  3. 3

    Render multiple components in React Router

  4. 4

    Spread an array of components in render in React

  5. 5

    How to progressively render react components?

  6. 6

    Render a string which contains components

  7. 7

    Is there a way to render multiple React components in the React.render() function?

  8. 8

    React js render html string returned from server

  9. 9

    Generate React components in PHP and then render in client

  10. 10

    how to render react components by using map and join

  11. 11

    Rendering React components with promises inside the render method

  12. 12

    How to dynamically load & render react components?

  13. 13

    react.js render components at different locations

  14. 14

    Render two components adjacent to each other in React

  15. 15

    Render multiple React components into a single DOM element

  16. 16

    React native button click render different components

  17. 17

    Re-render all components in React and Redux

  18. 18

    React data components table does not render HTML

  19. 19

    Use Tooltip.js to render React Components

  20. 20

    How to render components using function in React Native

  21. 21

    Render components inside other components with react.js

  22. 22

    How to test react components that render other components with props?

  23. 23

    Render JSF components directly from Javascript

  24. 24

    Render image from props in react,

  25. 25

    Express Render Template from String

  26. 26

    Render pug template from string?

  27. 27

    how to render child components in react.js recursively

  28. 28

    React Router doesn't render components after build to ftp server

  29. 29

    React: Abstraction for components. Composition vs Render props

HotTag

Archive