Fast 3kB alternative to React with the same modern API
function Counter() {
const [value, setValue] = useState(0);
return (
<>
<div>Counter: {value}</div>
<button onClick={() => setValue(value + 1)}>Increment</button>
<button onClick={() => setValue(value - 1)}>Decrement</button>
</>
)
}
Proudly sponsored by:
A different kind of library
Closer to the DOM
Preact provides the thinnest possible Virtual DOM abstraction on top of the DOM. It builds on stable platform features, registers real event handlers and plays nicely with other libraries.
Preact can be used directly in the browser without any transpilation steps.
Small Size
Most UI frameworks are large enough to be the majority of an app's JavaScript size. Preact is different: it's small enough that your code is the largest part of your application.
That means less JavaScript to download, parse and execute - leaving more time for your code, so you can build an experience you define without fighting to keep a framework under control.
Big Performance
Preact is fast, and not just because of its size. It's one of the fastest Virtual DOM libraries out there, thanks to a simple and predictable diff implementation.
We automatically batch updates and tune Preact to the extreme when it comes to performance. We work closely with browser engineers to get the maximum performance possible out of Preact.
Portable & Embeddable
Preact's tiny footprint means you can take the powerful Virtual DOM Component paradigm to new places it couldn't otherwise go.
Use Preact to build parts of an app without complex integration. Embed Preact into a widget and apply the same tools and techniques that you would to build a full app.
Instantly Productive
Lightweight is a lot more fun when you don't have to sacrifice productivity to get there. Preact gets you productive right away. It even has a few bonus features:
props
,state
andcontext
are passed torender()
- Use standard HTML attributes like
class
andfor
Ecosystem Compatible
Virtual DOM Components make it easy to share reusable things - everything from buttons to data providers. Preact's design means you can seamlessly use thousands of Components available in the React ecosystem.
Adding a simple preact/compat alias to your bundler provides a compatibility layer that enables even the most complex React components to be used in your application.
See it in action!
Todo List
export default class TodoList extends Component {
state = { todos: [], text: '' };
setText = e => {
this.setState({ text: e.currentTarget.value });
};
addTodo = () => {
let { todos, text } = this.state;
todos = todos.concat({ text });
this.setState({ todos, text: '' });
};
render({ }, { todos, text }) {
return (
<form onSubmit={this.addTodo} action="javascript:">
<label>
<span>Add Todo</span>
<input value={text} onInput={this.setText} />
</label>
<button type="submit">Add</button>
<ul>
{ todos.map( todo => (
<li>{todo.text}</li>
)) }
</ul>
</form>
);
}
}
Run in REPL
Running Example
import TodoList from './todo-list';
render(<TodoList />, document.body);
Fetch GitHub Stars
export default class Stars extends Component {
async componentDidMount() {
let stars = await githubStars(this.props.repo);
this.setState({ stars });
}
render({ repo }, { stars=0 }) {
let url = `https://github.com/${repo}`;
return (
<a href={url} class="stars">
⭐️ {stars} Stars
</a>
);
}
}
Run in REPL
Running Example
import Stars from './stars';
render(
<Stars repo="preactjs/preact" />,
document.body
);
Ready to dive in?
We've got separate guides based on whether you have experience with React.
Pick the guide that works best for you!