0

I crated a react html element like:

let elements = (
      <div>
        <div>dwewe</div>
        <div>wefwef</div>
        <span>yurhfjer</span>
      </div>

    );

and now I wanted to pass this to an html attribute, hence I converted the react element into string using:

<span data-tip-react={ReactDOMServer.renderToString(element)}>{title}></span>

I'm now able to access these elements, however I'd like to convert it back to react element (the way it was before conversion) here is what I'm expecting the o/p as:

enter image description here

I tried using DOMParser, however it returned an html element that React did not accept for rendering and threw an errr: not a react element

How do I convert the string back into the same format - React element?? please help! thanks

1
  • can you show this part I tried using DOMParser, however it returned an html element that React did not accept for rendering and threw an errr: not a react element Commented Mar 6, 2020 at 9:08

1 Answer 1

0

Following here :

dynamic HTML String to react component

You can use dangerouslySetInnerHTML (for simple element) or some npm package :

class App extends Component {
  constructor() {
    super();
    this.state = {
      name: 'React'
    };
  }

  getDom() {
    return (
      <div>
        <div>dwewe</div>
        <div>wefwef</div>
        <span>yurhfjer</span>
      </div>
    );
  }

  convertToString(dom) {
    console.log("To String", ReactDOMServer.renderToString(dom))
    return ReactDOMServer.renderToString(dom)
  }

  convertToDOM(string) {
    let domparser = new DOMParser();
    console.log("To Dom", domparser.parseFromString(string, 'text/html'))
    return domparser.parseFromString(string, 'text/html')​​
  }

  render() {
    return (
      <div>
        <Hello name={this.state.name} />
        <p>
          Start editing to see some magic happen :)
          {<div dangerouslySetInnerHTML={{__html: this.convertToString(this.getDom())}}></div>}
        </p>
      </div>
    );
  }
}

ex : https://stackblitz.com/edit/react-mhsqfd

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

6 Comments

Thanks for the solution: This is what I tried for DOM purify: let parser = new DOMParser(); let parsedHtml = parser.parseFromString(htmlContent, 'text/html') and I'm trying to render it as react HTML for which I get this error:
Error: Objects are not valid as a React child (found: object with keys {__html}). If you meant to render a collection of children, use an array instead.
also is there any alternative for using dangerouslySetInnerHTML, this is prone to xxs attacks and we are suing sanitize in our app for purifying any html tag strings..so I'm sure if this will work.. :(
@user1234 sorry for the delay, do you still have the problem? If so can you make a quick stackblitz so that it is easier to understand ?
Then the fast way would be to use something like react-jsx-parser or do by hand the convertion to react code using babel like stackoverflow.com/questions/36104302/…
|

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.