0

I want to know how to store an html tag as a Javascript object. Consider the following code:

const temp = () => {
    const complexity = "O(n<sup>2</sup>)"
    return (
        <div>
            {complexity}
        </div>
    )
}

Right now, it renders as the exact string: O(n<sup>2</sup>)
Instead I want the <sup> to be treated as a the html tag and render as: O(n2)

Any help is appreciated. Thanks

0

3 Answers 3

2

Don't use a string in the first place, use JSX:

const complexity = <React.Fragment>O(n<sup>2</sup>)</React.Fragment>

The React.Fragment is only needed here because you have three top-level nodes (text, element, text).

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

Comments

1

This is not working because its not HTML its jsx. jsx must retun a single element..so just wrap your <sup> tag inside any other tag

const Test = () => {
const complexity = <span>O(n<sup>2</sup>)</span>
return (
    <div>
        {complexity}
    </div>
)

}

Comments

0

I would recommend you to wrap them with empty fragments instead. E.g.

const complexity = <>O(n<sup>2</sup>)</>

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.