2

I am trying to build a react app with customized components. The input to the app is a jsx(as a string) coming from an API. Now i want to render this input jsx (a string variable), but not able to do that. I have tried eval() and dangerouslySetInnerHTML options, but it did not work out. My JSX String looks like

'<div> <MyComponent attr_1={this.state.array["field"]}></MyComponent> </div>'

3
  • dangerouslySetInnerHTML is the way to do this. Please show how you're trying to do that Commented May 10, 2020 at 13:12
  • 1
    Does this answer your question? How to render a string with JSX in React Commented May 10, 2020 at 13:15
  • @jayce444 " <div dangerouslySetInnerHTML={{__html: str}}></div> );" Commented May 10, 2020 at 13:23

1 Answer 1

2

Hi I figured out JsxParser is the right way of solving my problem. react-jsx-parser provides capability to parser and render react component from JSX string.

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

Comments

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.