Fast 3kB alternative to React with the same modern API.
**All the power of Virtual DOM components, without the overhead:** - Familiar React API & patterns: [ES6 Class] and [Functional Components] - Extensive React compatibility via a simple [preact-compat] alias - Everything you need: JSX, VDOM, React DevTools, HMR, SSR.. - A highly optimized diff algorithm and seamless Server Side Rendering - Transparent asynchronous rendering with a pluggable scheduler - 🆕💥 **Instant no-config app bundling with [Preact CLI](https://github.com/developit/preact-cli)** ### 💁 More information at the [Preact Website ➞](https://preactjs.com) --- - [Demos](#demos) - [Libraries & Add-ons](#libraries--add-ons) - [Getting Started](#getting-started) - [Import what you need](#import-what-you-need) - [Rendering JSX](#rendering-jsx) - [Components](#components) - [Props & State](#props--state) - [Linked State](#linked-state) - [Examples](#examples) - [Extensions](#extensions) - [Debug Mode](#debug-mode) - [Backers](#backers) - [Sponsors](#sponsors) - [License](#license) # Preact [](http://npm.im/preact) [](https://cdnjs.com/libraries/preact) [](https://preact-slack.now.sh) [](#backers) [](#sponsors) [](https://travis-ci.org/developit/preact) [](https://coveralls.io/github/developit/preact) [](https://unpkg.com/preact/dist/preact.min.js) [](https://packagephobia.now.sh/result?p=preact) Preact supports modern browsers and IE9+: [](https://saucelabs.com/u/preact) --- ## Demos #### Real-World Apps - [**Preact Hacker News**](https://hn.kristoferbaxter.com) _([GitHub Project](https://github.com/kristoferbaxter/preact-hn))_ - [**Play.cash**](https://play.cash) :notes: _([GitHub Project](https://github.com/feross/play.cash))_ - [**BitMidi**](https://bitmidi.com/) 🎹 Wayback machine for free MIDI files _([GitHub Project](https://github.com/feross/bitmidi.com))_ - [**Ultimate Guitar**](https://www.ultimate-guitar.com) 🎸speed boosted by Preact. - [**ESBench**](http://esbench.com) is built using Preact. - [**BigWebQuiz**](https://bigwebquiz.com) _([GitHub Project](https://github.com/jakearchibald/big-web-quiz))_ - [**Nectarine.rocks**](http://nectarine.rocks) _([GitHub Project](https://github.com/developit/nectarine))_ :peach: - [**TodoMVC**](https://preact-todomvc.surge.sh) _([GitHub Project](https://github.com/developit/preact-todomvc))_ - [**OSS.Ninja**](https://oss.ninja) _([GitHub Project](https://github.com/developit/oss.ninja))_ - [**GuriVR**](https://gurivr.com) _([GitHub Project](https://github.com/opennewslabs/guri-vr))_ - [**Color Picker**](https://colors.now.sh) _([GitHub Project](https://github.com/lukeed/colors-app))_ :art: - [**Offline Gallery**](https://use-the-platform.com/offline-gallery/) _([GitHub Project](https://github.com/vaneenige/offline-gallery/))_ :balloon: - [**Periodic Weather**](https://use-the-platform.com/periodic-weather/) _([GitHub Project](https://github.com/vaneenige/periodic-weather/))_ :sunny: - [**Rugby News Board**](http://nbrugby.com) _[(GitHub Project)](https://github.com/rugby-board/rugby-board-node)_ - [**Preact Gallery**](https://preact.gallery/) an 8KB photo gallery PWA built using Preact. - [**Rainbow Explorer**](https://use-the-platform.com/rainbow-explorer/) Preact app to translate real life color to digital color _([Github project](https://github.com/vaneenige/rainbow-explorer))_. - [**YASCC**](https://carlosqsilva.github.io/YASCC/#/) Yet Another SoundCloud Client _([Github project](https://github.com/carlosqsilva/YASCC))_. - [**Journalize**](https://preact-journal.herokuapp.com/) 14k offline-capable journaling PWA using preact. _([Github project](https://github.com/jpodwys/preact-journal))_. #### Runnable Examples - [**Flickr Browser**](http://codepen.io/developit/full/VvMZwK/) (@ CodePen) - [**Animating Text**](http://codepen.io/developit/full/LpNOdm/) (@ CodePen) - [**60FPS Rainbow Spiral**](http://codepen.io/developit/full/xGoagz/) (@ CodePen) - [**Simple Clock**](http://jsfiddle.net/developit/u9m5x0L7/embedded/result,js/) (@ JSFiddle) - [**3D + ThreeJS**](http://codepen.io/developit/pen/PPMNjd?editors=0010) (@ CodePen) - [**Stock Ticker**](http://codepen.io/developit/pen/wMYoBb?editors=0010) (@ CodePen) - [*Create your Own!*](https://jsfiddle.net/developit/rs6zrh5f/embedded/result/) (@ JSFiddle) ### Starter Projects - [**Preact Boilerplate**](https://preact-boilerplate.surge.sh) _([GitHub Project](https://github.com/developit/preact-boilerplate))_ :zap: - [**Preact Offline Starter**](https://preact-starter.now.sh) _([GitHub Project](https://github.com/lukeed/preact-starter))_ :100: - [**Preact PWA**](https://preact-pwa-yfxiijbzit.now.sh/) _([GitHub Project](https://github.com/ezekielchentnik/preact-pwa))_ :hamburger: - [**Parcel + Preact + Unistore Starter**](https://github.com/hwclass/parcel-preact-unistore-starter) - [**Preact Mobx Starter**](https://awaw00.github.io/preact-mobx-starter/) _([GitHub Project](https://github.com/awaw00/preact-mobx-starter))_ :sunny: - [**Preact Redux Example**](https://github.com/developit/preact-redux-example) :star: - [**Preact Redux/RxJS/Reselect Example**](https://github.com/continuata/preact-seed) - [**V2EX Preact**](https://github.com/yanni4night/v2ex-preact) - [**Preact Coffeescript**](https://github.com/crisward/preact-coffee) - [**Preact + TypeScript + Webpack**](https://github.com/k1r0s/bleeding-preact-starter) - [**0 config => Preact + Poi**](https://github.com/k1r0s/preact-poi-starter) - [**Zero configuration => Preact + Typescript + Parcel**](https://github.com/aalises/preact-typescript-parcel-starter) --- ## Libraries & Add-ons - :raised_hands: [**preact-compat**](https://git.io/preact-compat): use any React library with Preact *([full example](http://git.io/preact-compat-example))* - :twisted_rightwards_arrows: [**preact-context**](https://github.com/valotas/preact-context): React's `createContext` api for Preact - :page_facing_up: [**preact-render-to-string**](https://git.io/preact-render-to-string): Universal rendering. - :eyes: [**preact-render-spy**](https://github.com/mzgoddard/preact-render-spy): Enzyme-lite: Renderer with access to the produced virtual dom for testing. - :loop: [**preact-render-to-json**](https://git.io/preact-render-to-json): Render for Jest Snapshot testing. - :earth_americas: [**preact-router**](https://git.io/preact-router): URL routing for your components - :bookmark_tabs: [**preact-markup**](https://git.io/preact-markup): Render HTML & Custom Elements as JSX & Components - :satellite: [**preact-portal**](https://git.io/preact-portal): Render Preact components into (a) SPACE :milky_way: - :pencil: [**preact-richtextarea**](https://git.io/preact-richtextarea): Simple HTML editor component - :bookmark: [**preact-token-input**](https://github.com/developit/preact-token-input): Text field that tokenizes input, for things like tags - :card_index: [**preact-virtual-list**](https://github.com/developit/preact-virtual-list): Easily render lists with millions of rows ([demo](https://jsfiddle.net/developit/qqan9pdo/)) - :repeat: [**preact-cycle**](https://git.io/preact-cycle): Functional-reactive paradigm for Preact - :triangular_ruler: [**preact-layout**](https://download.github.io/preact-layout/): Small and simple layout library - :thought_balloon: [**preact-socrates**](https://github.com/matthewmueller/preact-socrates): Preact plugin for [Socrates](http://github.com/matthewmueller/socrates) - :rowboat: [**preact-flyd**](https://github.com/xialvjun/preact-flyd): Use [flyd](https://github.com/paldepind/flyd) FRP streams in Preact + JSX - :speech_balloon: [**preact-i18nline**](https://github.com/download/preact-i18nline): Integrates the ecosystem around [i18n-js](https://github.com/everydayhero/i18n-js) with Preact via [i18nline](https://github.com/download/i18nline). - :microscope: [**preact-jsx-chai**](https://git.io/preact-jsx-chai): JSX assertion testing _(no DOM, right in Node)_ - :tophat: [**preact-classless-component**](https://github.com/ld0rman/preact-classless-component): create preact components without the class keyword - :hammer: [**preact-hyperscript**](https://github.com/queckezz/preact-hyperscript): Hyperscript-like syntax for creating elements - :white_check_mark: [**shallow-compare**](https://github.com/tkh44/shallow-compare): simplified `shouldComponentUpdate` helper. - :shaved_ice: [**preact-codemod**](https://github.com/vutran/preact-codemod): Transform your React code to Preact. - :construction_worker: [**preact-helmet**](https://github.com/download/preact-helmet): A document head manager for Preact - :necktie: [**preact-delegate**](https://github.com/NekR/preact-delegate): Delegate DOM events - :art: [**preact-stylesheet-decorator**](https://github.com/k1r0s/preact-stylesheet-decorator): Add Scoped Stylesheets to your Preact Components - :electric_plug: [**preact-routlet**](https://github.com/k1r0s/preact-routlet): Simple `Component Driven` Routing for Preact using ES7 Decorators - :fax: [**preact-bind-group**](https://github.com/k1r0s/preact-bind-group): Preact Forms made easy, Group Events into a Single Callback - :hatching_chick: [**preact-habitat**](https://github.com/zouhir/preact-habitat): Declarative Preact widgets renderer in any CMS or DOM host ([demo](https://codepen.io/zouhir/pen/brrOPB)). - :tada: [**proppy-preact**](https://github.com/fahad19/proppy): Functional props composition for Preact components #### UI Component Libraries > Want to prototype something or speed up your development? Try one of these toolkits: - [**preact-material-components**](https://github.com/prateekbh/preact-material-components): Material Design Components for Preact ([website](https://material.preactjs.com/)) - [**preact-mdc**](https://github.com/BerndWessels/preact-mdc): Material Design Components for Preact ([demo](https://github.com/BerndWessels/preact-mdc-demo)) - [**preact-mui**](https://git.io/v1aVO): The MUI CSS Preact library. - [**preact-photon**](https://git.io/preact-photon): build beautiful desktop UI with [photon](http://photonkit.com) - [**preact-mdl**](https://git.io/preact-mdl): [Material Design Lite](https://getmdl.io) for Preact - [**preact-weui**](https://github.com/afeiship/preact-weui): [Weui](https://github.com/afeiship/preact-weui) for Preact --- ## Getting Started > 💁 _**Note:** You [don't need ES2015 to use Preact](https://github.com/developit/preact-in-es3)... but give it a try!_ The easiest way to get started with Preact is to install [Preact CLI](https://github.com/developit/preact-cli). This simple command-line tool wraps up the best possible Webpack and Babel setup for you, and even keeps you up-to-date as the underlying tools change. Best of all, it's easy to understand! It builds your app in a single command (`preact build`), doesn't need any configuration, and bakes in best-practises 🙌. The following guide assumes you have some sort of ES2015 build set up using babel and/or webpack/browserify/gulp/grunt/etc. You can also start with [preact-boilerplate] or a [CodePen Template](http://codepen.io/developit/pen/pgaROe?editors=0010). ### Import what you need The `preact` module provides both named and default exports, so you can either import everything under a namespace of your choosing, or just what you need as locals: ##### Named: ```js import { h, render, Component } from 'preact'; // Tell Babel to transform JSX into h() calls: /** @jsx h */ ``` ##### Default: ```js import preact from 'preact'; // Tell Babel to transform JSX into preact.h() calls: /** @jsx preact.h */ ``` > Named imports work well for highly structured applications, whereas the default import is quick and never needs to be updated when using different parts of the library. > > Instead of declaring the `@jsx` pragma in your code, it's best to configure it globally in a `.babelrc`: > > **For Babel 5 and prior:** > > ```json > { "jsxPragma": "h" } > ``` > > **For Babel 6:** > > ```json > { > "plugins": [ > ["transform-react-jsx", { "pragma":"h" }] > ] > } > ``` > > **For Babel 7:** > > ```json > { > "plugins": [ > ["@babel/plugin-transform-react-jsx", { "pragma":"h" }] > ] > } > ``` > **For using Preact along with TypeScript add to `tsconfig.json`:** > > ```json > { > "jsx": "react", > "jsxFactory": "h", > } > ``` ### Rendering JSX Out of the box, Preact provides an `h()` function that turns your JSX into Virtual DOM elements _([here's how](http://jasonformat.com/wtf-is-jsx))_. It also provides a `render()` function that creates a DOM tree from that Virtual DOM. To render some JSX, just import those two functions and use them like so: ```js import { h, render } from 'preact'; render((