0
html = "<p>Hello {this.getValue("a")}</p>"

Basically what I want to achieve is the value returned by the getValue() function once the html is rendered.

I tried react HTML parser however this shows the function name as it is written in the code.

I also tried dangerouslySetInnerHTML like below:

<div dangerouslySetInnerHTML={{ __html: html }} />

The result with "dangerouslySetInnerHTML" is the same string

Hello {this.getValue("a")}

Also the string is from an api response

example: "<div class="test"><span>Overview</span></div><p><b>testing testing</b></p><p>{this.getValue('a')}, testing</p>"

1 Answer 1

1

Use template literals - use backticks `` and ${}:

const getValue = arg => arg + arg;
const html = `<p>Hello ${getValue("a")}</p>`;
document.write(html);

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

4 Comments

Just to explain further: the string that I am getting is from an api response, will it then be possible to use template literals and back ticks?
You could use replace to do that
Okay, lemme try that
I tried that however it's replacing all the other double quotes, the response from api is like this "<div class="test"><span>Overview</span></div><p><b>testing testing</b></p><p>{this.getValue('a')}, testing</p>"

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.