0

I'm trying to put a html tag inside a span to give some styling to a certain word. but it results in a plain text instead.

expectation:

abc AND xyz

what I get instead:

abc <strong>AND</strong> xyz

here's my code: ( codesandbox )

import { useState } from "react";

export default function App() {
  const [isLoading, setIsLoading] = useState(false);

  return (
    <div className="App">
      <h1>Hello CodeSandbox</h1>
      <div>
        <span>{isLoading ? "abc" : `abc <strong>AND</strong> xyz`}</span>
      </div>
    </div>
  );
}

2 Answers 2

4

You can just add it like this

<span>{isLoading ? "abc" : (<>abc <strong>AND</strong> xyz</>)}</span>
Sign up to request clarification or add additional context in comments.

Comments

0

One issue with your approach is that there are multiple adjacent DOM nodes. You can wrap them using a fragment:

<>abc <strong>AND</strong> xyz</>

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.