3

I have a string which is actually a nested react components name with its props. Want to render that string inside another react component . For example

var String = "<parentComponent title='Parent'><childComponent age='23'></childComponent></parentComponent>"

React Code: -

class MainComponnet extends Component{
 render(){
  let stringComponents = "<parentComponent title='Parent'><childComponent age='23'></childComponent></parentComponent>";
  return(
     {stringComponents}
  )
 }
}

Parent Component JSX : -

class ParentComponent extends Component{
 render(){
  return(
     <div> {this.props.title}</div>
  )
 }
}

Child Component JSX : -

class ChildComponent extends Component{
 render(){
  return(
     <div> {this.props.age}</div>
  )
 }
}

Please help..

6
  • One question why is it inside a string? Commented Jun 16, 2017 at 13:47
  • Actually service would be returning the component string and accordingly I have to render that components. Commented Jun 16, 2017 at 13:54
  • I think doing this is somewhat not possible.Probably you need a react-jsx-parser here to convert you string to a normal component type thing and then render it Commented Jun 16, 2017 at 14:04
  • Thanks Vivek. Am hunting for such parser online, hard luck . Is there any way that you can help me Commented Jun 16, 2017 at 14:11
  • Not sure, but can you try this github.com/TroyAlford/react-jsx-parser Commented Jun 16, 2017 at 14:13

2 Answers 2

1

Thanks alot for every one for helping me. Library 'react-jsx-parser' solved my problem .

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

Comments

0

That string has JSX content, you can directly render JSX contents, also the component names must begin with Uppercase charater.

See this answer: React - Adding component after AJAX to view

class MainComponnet extends Component{
 render(){
  let stringComponents = <ParentComponent title='Parent'><ChildComponent age='23'></ChildComponent></ParentComponent>;
  return(
     </div>{stringComponents}</div>
  )
 }
}

In case you cannot use a direct JSX element, you can transform your string to JSX using babel. However its not a good idea. you should rather modify your logic.

You can do it as follows

import babel from 'babel-core';
var Component = eval(babel.transform('<ParentComponent title='Parent'><ChildComponent age='23'></ChildComponent></ParentComponent>).code);

4 Comments

Thanks for Reply @Shubham but if you will notice my string is surronded by double quotes and yours not. Its data type is string hence while render its not processing react component.
Exactly my point, you dont need to surround your string within double quotes, directly assign the JSX content to the variable
I don't have any other options, this is what am getting in json from backend. If I can remove that quotes on UI or any other way to load component from String would be helpful.
Added that part in the answer

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.