Skip to content

Safe JSX: syntactic sugar over dangerouslySetInnerHTML

License

Notifications You must be signed in to change notification settings

ivan-kleshnin/react-safe

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

React Safe

This package provides a syntactic sugar over the raw dangerousSetInnerHTML.

Motivation

In real projects, at least in my experience, the usage of dangerousSetInnerHTML is extensive. Which brings two problems:

  1. It's too long and ugly for its frequency. Also JSX does not look like HTML anymore as tag contents are passed via attributes. Which kinda defeats the usage point of JSX.

  2. The term "dangerous" is misleading. It represents something a programmer considers safe(!) instead. So it kinda spams the vision with irrelevant signals of false danger decreasing the capability to notice real threats.

Examples

React (vanilla)

<h1 dangerouslySetInnerHTML={{__html: page.title}}></h1>
<div dangerouslySetInnerHTML={{__html: renderMD(page.body)}}></div>

React Safe

<Safe.h1>{page.title}</Safe.h1>
<Safe.div>{renderMD(page.body)}</Safe.div>

Warning

React-Safe is a thin wrapper over dangerouslySetInnerHTML so all the usual concerns about XSS attacks and security in general apply. Check the above link for more information.

License

MIT

About

Safe JSX: syntactic sugar over dangerouslySetInnerHTML

Resources

License

Stars

Watchers

Forks

Packages

No packages published