Skip to main content
Module

x/keywork/react/mod.ts

A library for building V8 Isolate web apps on Cloudflare Workers, Deno, and Node.JS
Latest
import * as keywork from "https://deno.land/x/keywork@v6.2.1/react/mod.ts";

While optional, Keywork uses React as its primary HTML templating engine.

Peer Dependencies

If you haven't already, make sure to add the React peer dependencies:

$ yarn add react react-dom
$ npm install --save react react-dom

Import Maps are recommended to avoid long import URLs:

{
  "imports": {
    "react": "https://esm.sh/react@18.2.0",
    "react-dom": "https://esm.sh/react-dom@18.2.0",
    "react-dom/client": "https://esm.sh/react-dom@18.2.0/client",
    "react-dom/server": "https://esm.sh/react-dom@18.2.0/server",
    "react-dom/server.browser": "https://esm.sh/react-dom@18.2.0/server.browser",
    "react/jsx-runtime": "https://esm.sh/react/jsx-runtime"
  }
}

Usage

Route handlers defined on an instance of Keywork#Router.RequestRouter | RequestRouter can return a React component, Keywork automatically converts the content into a streamed response.

import { RequestRouter } from 'keywork/router'

const app = new RequestRouter()

app.get('/', () => <h1>Hello from Keywork! 👋</h1>)

interface GreetParams {
  firstName: string
}

app.get('/greet/:firstName', ({ params }) => {
  return (
    <div>
      <h1>Hello there! {params.firstName}</h1>
    </div>
  )
})

export default app

Variables

Embeds the given SSR props in the DOM for client-side hydration.

The global key where SSR props are assigned. This includes a ':' character to prevent document.querySelector from matching this key.

A server-side render of a given HTML document.

The default ID assigned to the React root element.

The default ID assigned to the React root style element.

Functions

A convenience wrapper around React.createContext that creates a context and hook for the child components within a Provider's context.

Renders the given React content to an HTML stream.

Type Aliases

A component which wraps the current SSR routes. Use this if you need to inject a provider into the SSR pipeline.