Dev.to WebDev 🛠 Dev 👁 0 📖 1 min read

Preact for Beginners: React's API in 3 KB

Preact is a 3 KB alternative to React with the same API. If you know React — components, hooks, JSX, props — you already know Preact. The difference is size: where React plus ReactDOM is around 40 KB, Preact is about 3 K

Preact is a 3 KB alternative to React with the same API. If you know React — components, hooks, JSX, props — you already know Preact. The difference is size: where React plus ReactDOM is around 40 KB, Preact is about 3 KB. For pages where every kilobyte matters, that difference is the whole point.

Preact is not a toy. It powers real production sites, and its compatibility layer (preact/compat) lets most React libraries run unchanged. You can even alias react to preact/compat in your bundler and keep using your favourite React packages.

npm install preact
/** @jsx h */
import { h, render } from 'preact';
import { useState } from 'preact/hooks';

function Counter() {
  const [count, setCount] = useState(0);
  return <button onClick={() => setCount(count + 1)}>Clicked {count} times</button>;
}

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

Preact is a great fit for embedded widgets, marketing pages with a little interactivity, and performance-critical sites. It is also a good choice when you want React's mental model without React's bundle cost.

The limits are small but real. Preact's ecosystem is React's ecosystem only through the compat layer, and a few edge-case React behaviours differ. Server components and the newest React features arrive in Preact later, if at all. And if your team already knows React, switching saves kilobytes but adds a small learning cost.

Try Preact the next time a page needs one interactive widget: drop in a 3 KB component instead of a 40 KB framework, and watch your Lighthouse score move.

📰 Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.