0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-23 21:53:55 -05:00
astro/packages/integrations/preact
Aditya Jain 620f1d9e1e
fixed broken emoji for in "Getting started" section of integration docs (#4795)
* fixed broken emoji for "nest frameworks together"

* Add missing whitespace

Co-authored-by: Yan Thomas <61414485+Yan-Thomas@users.noreply.github.com>
2022-09-19 12:58:06 -03:00
..
src Automatically set up Preact DevTools in dev mode (#4515) 2022-09-07 13:49:07 -05:00
CHANGELOG.md [ci] release (#4657) 2022-09-07 21:29:44 +00:00
client-dev.js [ci] format 2022-09-07 18:50:48 +00:00
client.js [ci] format 2022-06-23 15:12:46 +00:00
package.json [ci] release (#4657) 2022-09-07 21:29:44 +00:00
README.md fixed broken emoji for in "Getting started" section of integration docs (#4795) 2022-09-19 12:58:06 -03:00
server.js [ci] format 2022-06-30 12:12:27 +00:00
static-html.js Enable named slots in renderers (#3652) 2022-06-23 10:10:54 -05:00
tsconfig.json Astro Integration System (#2820) 2022-03-18 15:35:45 -07:00

@astrojs/preact ⚛️

This Astro integration enables server-side rendering and client-side hydration for your Preact components.

Why Preact?

Preact is a library that lets you build interactive UI components for the web. If you want to build interactive features on your site using JavaScript, you may prefer using its component format instead of using browser APIs directly.

Preact is also a great choice if you have previously used React. Preact provides the same API as React, but in a much smaller 3kB package. It even supports rendering many React components using the compat configuration option (see below).

Want to learn more about Preact before using this integration?
Check out “Learn Preact in 10 minutes”, an interactive tutorial on their website.

Installation

Quick Install

The astro add command-line tool automates the installation for you. Run one of the following commands in a new terminal window. (If you aren't sure which package manager you're using, run the first command.) Then, follow the prompts, and type "y" in the terminal (meaning "yes") for each one.

# Using NPM
npx astro add preact
# Using Yarn
yarn astro add preact
# Using PNPM
pnpm astro add preact

If you run into any issues, feel free to report them to us on GitHub and try the manual installation steps below.

Manual Install

First, install the @astrojs/preact package using your package manager. If you're using npm or aren't sure, run this in the terminal:

npm install @astrojs/preact

Most package managers will install associated peer dependencies as well. Still, if you see a "Cannot find package 'preact'" (or similar) warning when you start up Astro, you'll need to install Preact:

npm install preact

Then, apply this integration to your astro.config.* file using the integrations property:

astro.config.mjs

import { defineConfig } from 'astro/config';
import preact from '@astrojs/preact';

export default defineConfig({
  // ...
  integrations: [preact()],
});

Usage

To use your first Preact component in Astro, head to our UI framework documentation. You'll explore:

  • 📦 how framework components are loaded,
  • 💧 client-side hydration options, and
  • 🤝 opportunities to mix and nest frameworks together

Also check our Astro Integration Documentation for more on integrations.

Configuration

The Astro Preact integration handles how Preact components are rendered and it has its own options. Change these in the astro.config.mjs file which is where your project's integration settings live.

For basic usage, you do not need to configure the Preact integration.

compat

You can enable preact/compat, Preacts compatibility layer for rendering React components without needing to install or ship Reacts larger libraries to your users web browsers.

To do so, pass an object to the Preact integration and set compat: true.

// astro.config.mjs
import { defineConfig } from 'astro/config';
import preact from '@astrojs/preact';

export default defineConfig({
  integrations: [
    preact({ compat: true })
  ],
});

With the compat option enabled, the Preact integration will render React components as well as Preact components in your project and also allow you to import React components inside Preact components. Read more in “Switching to Preact (from React)” on the Preact website.

When importing React component libraries, in order to swap out the react and react-dom dependencies as preact/compat, you can use overrides to do so.

// package.json
{
  "overrides": {
    "react": "npm:@preact/compat@latest",
    "react-dom": "npm:@preact/compat@latest"
  }
}

Check out the pnpm overrides and yarn resolutions docs for their respective overrides features.

Note

Currently, the compat option only works for React libraries that export code as ESM. If an error happens during build-time, try adding the library to vite.ssr.noExternal: ['the-react-library'] in your astro.config.mjs file.

Examples

  • The Astro Preact example shows how to use an interactive Preact component in an Astro project.
  • The Astro Nanostores example shows how to share state between different components — and even different frameworks! — in an Astro project.

Troubleshooting

For help, check out the #support-threads channel on Discord. Our friendly Support Squad members are here to help!

You can also check our Astro Integration Documentation for more on integrations.

Contributing

This package is maintained by Astro's Core team. You're welcome to submit an issue or PR!

Changelog

See CHANGELOG.md for a history of changes to this integration.