← All Articles A Product of Kinsa Creative

Escape HTML in React using HTML-Entities

The html-entities NPM package allows HTML character decoding for things like ampersands (&) in react.

Once installed (npm install html-entities), characters can be decoded as follows in a React Component definition. In this example, we have a varaible, name which is being decoded if it is present.

import {decode} from html-entities;

...

return (
    ...

    <div>{name ? decode(`${name}`) : ''}</div>

    ...
);

Feedback?

Email us at enquiries@kinsa.cc.