Skip to content

Example for Preact

Since the routing library is a complex integration package that requires a server for SSR and work with the History API, Live Preview is unavailable. However, you can download ready-made examples.

Preview

src
components
pages
client.tsx
declarations.d.ts
router.ts
server.tsx
style.module.css
template.html
build.ts
package.json
plugins.ts
tsconfig.json
tsx
import { hydrate, render } from 'preact';

import { App } from './components/App';
import { getRouter, RouterContext } from './router';

const router = getRouter();

await router.init(location.href, { skipLifecycle: Boolean(SSR_ENABLED) });

if (SSR_ENABLED) {
  hydrate(
    <RouterContext.Provider value={{ router }}>
      <App />
    </RouterContext.Provider>,
    document.getElementById('example-app')!
  );

  console.log('SSR: App has been hydrated, no lifecycle called');
} else {
  render(
    <RouterContext.Provider value={{ router }}>
      <App />
    </RouterContext.Provider>,
    document.getElementById('example-app')!
  );

  console.log('CSR: App has been rendered and lifecycle called');
}

Download

shell
npx degit dkazakov8/reactive-route/examples/preact preact-example
cd preact-example
npm install

Then choose the mode and reactivity system to run:

  • npm run mobx — CSR (client-side rendering only) for MobX
  • npm run observable — CSR (client-side rendering only) for Observable
  • npm run ssr-mobx — SSR for MobX
  • npm run ssr-observable — SSR for Observable

Note that in this example, wrapping components in observer is handled by the ESBuild bundler. In your own projects, do not forget to follow the relevant integration guide.

No AI participated in the development. MIT License