0

I am looking for the equivalent of creating of HTML Tags step by step (concatenating them) like below in React:

var result="";
result += "<div>"; 
result += "dd";
result += "</div>"; 

Something like the following, but this is not working. I tried with an array, but it did also not work...

function MyComponent(props) {

   var result;
   result += <div>; 
   result += "dd";
   result += </div>;   

   return result; 
}

Thanks very much!

1 Answer 1

1

Try using dangerouslySetInnerHTML, official example:

function createMarkup() {
  return {__html: 'First &middot; Second'};
}

function MyComponent() {
  return <div dangerouslySetInnerHTML={createMarkup()} />;
}

In your case:

function MyComponent(props) {

   var result;
   result += "<div>"; 
   result += "dd";
   result += "</div>";   

   return <div dangerouslySetInnerHTML={{__html: result}}></div>; 
}

Additionally, you can look for any string-to-jsx library for use.

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.