1

I'm getting an HTML string from an AJAX request, that looks something like that:

<div> <SpecialComponent/> <SecondSpecialComponent/></div>

What i need, is to be able use this string in a React component, as it were valid JSX.

I tried using the dangerouslySetInnerHTML as instructed in this discussion: how-to-parse-html-to-react-component

I've also tried React libraries like react-html-parse and html-react-parser. No success

When i was working with AngularJS(1), i was using some small directive that would take care of that situation. I need to achieve the same with React. Any ideas?

EDIT: if anybody is interested, i found a library that takes care of the issue: https://www.npmjs.com/package/react-jsx-parser

9
  • can you post the html string or small template here , because if it was html using dangerouslySetInnerHTML should work fine Commented Mar 21, 2018 at 14:30
  • What was wrong with using dangerouslySetInnerHTML? Commented Mar 21, 2018 at 14:36
  • What you are trying to do requires the content to be transpiled because it contains jsx. This will be very hard. Instead if you control the endpoint you are better off only returning json serializable data that can be rendered by components. Commented Mar 21, 2018 at 14:43
  • to Stretch0: it just didn't work, no matter how i tried. also in this example here it doesn't work: codesandbox.io/s/WnKvoY6BE. to trixin: well this would undermine the whole idea behind what im trying to build...so, i need to make it work :-) Commented Mar 21, 2018 at 14:45
  • 1
    @TomaszBubała No it doesn't work fine. If you inspect the DOM you will see that it didn't render a component but an invalid html node <specialcomponent>. Depending on the browser it may render it anyways. But it's not a react component at all. This is just due to the fact that jsx can look like valid html markup but it isn't. Commented Mar 21, 2018 at 15:16

2 Answers 2

-1

You're not supposed to be doing things this way. Although React components look like html tags when using JSX, they are actually either classes or functions. What you're trying to do is equivalent to trying to parse:

"<div>document.write('foo')</div>"

You're literally mixing a javascript function name in string form with html markup in the same string. You will have to parse the string to separate the React component from the surrounding html markup, map the React components in string form to their actual React counterparts, and use ReactDOM.render to add the component to the page.

let mapping = [
    {"name":"<SpecialComponent/><SecondSpecialComponent/>","component":<SpecialComponent/><SecondSpecialComponent/>},
    {"name":"<someOtherComponent/><someOtherComponent/>","component":<someOtherComponent/><someOtherComponent/>}
];
let markup = "<div><SpecialComponent/><SecondSpecialComponent/></div>";
let targetComponent;

mapping.forEach((obj) => {if(markup.indexOf(obj.name)>-1){
    targetComponent=obj.component;//acquired a reference to the actual component
    markup.replace(obj.name,"");//remove the component markup from the string
    }
});

/*place the empty div at the target location within the page 
give it an "id" attribute either using element.setAttribute("id","label") or 
by just modifying the string to add id="label" before the div is placed in 
the DOM ie, <div id='label'></div>*/

//finally add the component using the id assigned to it

ReactDOM.render(targetComponent,document.getElementById("label"));

Never tried this before. I wonder if this will actually work :)

Sign up to request clarification or add additional context in comments.

2 Comments

This may even work but it is very simplified. If I understand OP correctly he wants to use it in a fully fledged CMS system. There exist some libraries that can dynamically render jsx but it's more intended to be used as an interactive playground. react-live is one of them.
To be honest...i didn't really understand what's going on there. But i understand it's quite similar to what is described here, which i managed to implement: alexvab.com/posts/how-to-dynamically-render-react-js-components This is already a good start. i could create an object that holds references to all relevant components that i might need, and then do some manipulations on the HTML string that i receive from the server, to output those elements. Will be very messy to implement, being that i might need to pass props. in my AngularJS implementations it was all just parsed as is...
-2

my suggestion is you do something like the following.

first set a state variable to equal the HTML you want to return

this.setState({html: response.data.html})

then in your return you can do the following

   return ( 
   {this.state.html} 
   );

1 Comment

React escapes strings that contain html. This will render as a string and not as markup. Also in OPs case the markup is actually jsx and not html.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.