preactjs/preact

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

Familiar React API & patterns: ES6 Class, hooks, and Functional Components
Extensive React compatibility via a simple preact/compat alias
Everything you need: JSX, VDOM, DevTools, HMR, SSR.
Highly optimized diff algorithm and seamless hydration from Server Side Rendering
Supports all modern browsers and IE11
Transparent asynchronous rendering with a pluggable scheduler
Instant production-grade app setup with Preact CLI
tipping_hand_person More information at the Preact Website ➞
npm Preact Slack Community OpenCollective Backers OpenCollective Sponsors

coveralls gzip size brotli size

Browser support matrix
You can find some awesome libraries in the awesome-preact list sunglasses

Getting Started
tipping_hand_person Note: You don’t need ES2015 to use Preact… but give it a try!

The easiest way to get started with Preact is to install Preact CLI. This simple command-line tool wraps up the best possible tooling for you, and even keeps things like Webpack and Babel up-to-date as they change. Best of all, it’s easy to understand! Start a project or compile for production in a single command (preact build), with no configuration needed and best practices baked in! raised_hands

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(

Hello

, document.body);

// update the tree in-place:
render(

Hello World!

, document.body);
// ^ this second invocation of render(…) will use a single DOM call to update the text of the


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} onChange={e => setInput(e.target.value)} />
	</div>
)

}

render(, document.body);
Backers
Support us with a monthly donation and help us continue our activities. [Become a backer]

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

License
MIT

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值