preact README

The project's README from github.com/preactjs/preact. As one markdown file: README.md.

Preact

Fast 4kB alternative to React with the same modern API.

All the power of Virtual DOM components, without the overhead:

๐Ÿ’ More information at the Preact Website โžž

npm Preact Slack Community OpenCollective Backers OpenCollective Sponsors

coveralls gzip size brotli size

You can find some awesome libraries in the awesome-preact list :sunglasses:


Getting Started

๐Ÿ’ Note: You don't need ES2015 to use Preact... but give it a try!

Tutorial: Building UI with Preact

With Preact, you create user interfaces by assembling trees of components and elements. Components are functions or classes that return a description of what their tree should output. These descriptions are typically written in JSX (shown underneath), or HTM which leverages standard JavaScript Tagged Templates. Both syntaxes can express trees of elements with "props" (similar to HTML attributes) and children.

To get started using Preact, first look at the render() function. This function accepts a tree description and creates the structure described. Next, it appends this structure to a parent DOM element provided as the second argument. Future calls to render() will reuse the existing tree and update it in-place in the DOM. Internally, render() will calculate the difference from previous outputted structures in an attempt to perform as few DOM operations as possible.

import { h, render } from 'preact';
// Tells babel to use h for JSX. It's better to configure this globally.
// See https://babeljs.io/docs/en/babel-plugin-transform-react-jsx#usage
// In tsconfig you can specify this with the jsxFactory
/** @jsx h */

// create our tree and append it to document.body:
render(
	<main>
		<h1>Hello</h1>
	</main>,
	document.body
);

// update the tree in-place:
render(
	<main>
		<h1>Hello World!</h1>
	</main>,
	document.body
);
// ^ this second invocation of render(...) will use a single DOM call to update the text of the <h1>

Hooray! render() has taken our structure and output a User Interface! This approach demonstrates a simple case, but would be difficult to use as an application grows in complexity. Each change would be forced to calculate the difference between the current and updated structure for the entire application. Components can help here โ€“ by dividing the User Interface into nested Components each can calculate their difference from their mounted point. Here's an example:

import { render, h } from 'preact';
import { useState } from 'preact/hooks';

/** @jsx h */

const App = () => {
	const [input, setInput] = useState('');

	return (
		<div>
			<p>Do you agree to the statement: "Preact is awesome"?</p>
			<input value={input} onInput={e => setInput(e.target.value)} />
		</div>
	);
};

render(<App />, document.body);

Sponsors

Become a sponsor and get your logo on our README on GitHub with a link to your site. [Become a sponsor]

ย  ย  ย  ย  ย  ย  SentDM Songsterr Deno

Backers

Support us with a monthly donation and help us continue our activities. [Become a backer]


License

MIT

Preact

preact (preactjs/preact)

Searching Databaseโ€ฆ

38.9K ยทGitHub
What is this?Preact is a fast, 3kB alternative to React that implements the same modern API, including Components and Virtual DOM.
Is it a framework or a library, and how does it relate to React?It is a UI rendering library, not a full framework. It mirrors React's API (classes, hooks, functional components) and can be used as a drop-in replacement via the preact/compat alias.
How do I add Preact to a project?You write components and elements using JSX or HTM tagged templates, then call the render() function to mount your component tree into a DOM element.
Do I need to use JSX, or is there another syntax?JSX is the typical way to write components, but it's not required โ€” you can also use HTM, which uses standard JavaScript Tagged Templates instead.
Will my existing React code just work with Preact?Most React code should work via the preact/compat compatibility layer, but it's described as a simple alias rather than a perfect 1:1 reimplementation, so some edge cases in React-specific behavior may not be covered.
Does it support server-side rendering and hot reloading?Yes, Preact includes everything needed for SSR, hydration, DevTools integration, and HMR out of the box.
Which browsers does it support?Preact supports all modern browsers.
Why is Preact faster/smaller than React?It achieves its tiny ~3kB size and speed through a highly optimized diff algorithm and seamless hydration, while keeping the familiar React component model.
Does it support async rendering?Yes, Preact has transparent asynchronous rendering with a pluggable scheduler.
Where can I find community libraries and add-ons?The awesome-preact list curates community-built libraries and tools for use with Preact.
Creator
preactjs
Size
19.6 MB
Stars
38.9K
Age
11y
Commits ยท 90 Days