Categories
React

React – Render String with HTML tags as HTML

TRY IT LIVE

Click the button below to see a live example of this example that you can edit and try for yourself.

CODE SANDBOX

There are often times when you have a string with some HTML tags such as strong that you want to render as HTML on the DOM. Most solutions online recommend using dangerouslySetInnerHTML but that is dangerous as the name suggests. The proper way to render HTML from string is to use FormattedMessage from the formatjs library (react-intl version 5).

Here is a code example:

import React from "react";
import "./styles.css";
import { IntlProvider, FormattedMessage } from "react-intl";

export default function App() {
  return (
    <IntlProvider>
      <div className="App" style={{fontSize: 24}}>
        <FormattedMessage
          id="app.greeting"
          description="Bold text example"
          defaultMessage="Look here, I can include HTML tags in plain string and render them as HTML: <b>Bold</b>, <i>Italics</i> and <a>links too</a>."
          values={{
            b: (chunks) => <b>{chunks}</b>,
            i: (chunks) => <i>{chunks}</i>,
            a: (chunks) => (
              <a class="external_link" target="_blank" href="https://jiga.dev/">
                {chunks}
              </a>
            )
          }}
        />
      </div>
    </IntlProvider>
  );
}

This should show an output as:

Play with code sample in this link: https://codesandbox.io/s/react-render-html-from-props-string-c9ogm

You can also pass the defaultMessage and values as prop values from other components.

In the above example, I have used the version 5 from the react-intl library. In the previous versions, the API is a little different. Follow the official documentation for more options such as using child elements.

Categories
HTML

First look at HTML

HTML is used to feed the content on the web page or web application.

HTML = content.

HTML is not used for styling the page if you want to style then you need to use CSS.

HTML ≠ style. CSS = style.

HTML Document Structure

All HTML document must start with <html> and end with </html>.

<body> … </body> body tag is where the content is entered and visible in the browser.

<!DOCTYPE HTML>
<html>
<body>

<h1>My First Heading</h1>
<p>My first paragraph.</p>

</body>
</html>

If you don’t know how to run this code you can use this link and follow the steps.

HTML Headings :

  • <h1> </h1> tags are used to define the headings.
  • heading tags vary from <h1> to <h6>.
  • <h1> with the larger size and the size reduce when the number increases till <h6>.
<html>
<body>

<h1> This is h1 element </h1>
<h2> This is h2 element </h2>
<h3> This is h3 element </h3>
<h4> This is h4 element </h4>
<h5> This is h5 element </h5>
<h6> This is h6 element </h6>

</body>
</html>

you can copy paste this code and run it using editor.

HTML Paragraph:

<p> tags are used to define the HTML paragraph.

<html>
<body>

<p> This is an paragraph </p>
<p> this is an second paragraph </p>

</body>
</html>

you can copy paste this code and run it using editor.