Skip to main content

JSX Purify

Installation

npm install jsx-purify

Setup your JSX transpiler

To use JSX Purify, you need to setup your JSX transpiler to use the jsx-purify/react package as the JSX factory. This is done differently depending on the transpiler you are using.

tsconfig.json
{
"compilerOptions": {
"jsxImportSource": "jsx-purify/react"
}
}

Usage

After you setup the transpiler, all JSX created with jsx-purify Runtime will be safe to execute in a client browser.

untrusted.jsx
/** @jsxImportSource jsx-purify/react */

export function App() {
return (
<div>
<script>alert('Bad actor')</script>
<h1>Hello World</h1>
<img onerror={() => alert("Bad actor")} src="nonsense" />
<p>This is a paragraph</p>
</div>
);
}