Render primitives

Declarative rendering in plain JSX

Conditions, cases, lists and wrappers, expressed as components in the tree. No extra wrapper nodes. Works with Server Components.

status

<r.switch value={status}><r.switch.case is="loading"><Spinner /></r.switch.case><r.switch.case is="error"><Retry /></r.switch.case><r.switch.default><Projects /></r.switch.default></r.switch>
Loading projects

get started

npm install react-renderable

under 1 kB minified and gzipped

The idea

A screen is easier to follow when its rendering decisions are visible.

Ternaries, && and .map are useful, but they can make branches and empty states easy to miss. Renderable gives those decisions names: show, switch, each and more.

Each primitive is a function component. No hooks or context to configure: use the pieces you need, or the whole set.

The types follow your data. r.each infers the item type from items, and TypeScript catches missing required props.

See it in context

Follow the screen from top to bottom. Each rendering decision appears in the JSX where it takes effect. Switch to native JSX to compare both versions.

projects-screen.tsx
<r.wrap
  with={[
    <ThemeProvider />,
    <QueryProvider client={client} />,
    <IntlProvider locale="en" />,
    <ToastProvider />,
  ]}
>
  <r.inject before={<Header />} after={<Toaster />}>
    <r.guard when={error} then={<Redirect to="/error" />}>
      <r.switch value={status}>
        <r.switch.case is="loading">
          <Spinner />
        </r.switch.case>
        <r.switch.default>
          <r.each
            items={projects}
            map={(project) => <ProjectRow key={project.id} project={project} />}
            empty={<EmptyState />}
          />
        </r.switch.default>
      </r.switch>
      <r.show when={hasMore} fallback={<AllSet />}>
        <PaginatorLoader />
      </r.show>
    </r.guard>
  </r.inject>
</r.wrap>

The details

Ten primitives. One familiar tree. Use them for conditions, cases, lists and wrappers, right alongside your existing JSX.

Elements in, elements out

Pass rendered elements into slots, such as <Login />. For lists, map receives each item and returns its element.

No extra nodes

Most primitives add no wrapper to the rendered tree. Use within when you want to group the selected branch.

Server Component friendly

The primitives can render on the server. r.portal handles its client-side work internally, so you can keep using it from your own files.

Under 1 kB

Under 1 kB minified and gzipped. React and React DOM are peer dependencies; there are no other runtime dependencies.

In practice

Three things that usually take more code. Exit animations, data on the server and components that change their tag.

Animate every branch

With within, the chosen branch becomes a direct, keyed child of the element you pass. AnimatePresence sees steps enter and leave, with no keys to write.

<r.switch
  value={step}
  within={<AnimatePresence mode="wait" />}
>
  <r.switch.case is="account">
    <motion.div exit={{ opacity: 0 }}>
      <AccountStep />
    </motion.div>
  </r.switch.case>
  <r.switch.case is="billing">
    <motion.div exit={{ opacity: 0 }}>
      <BillingStep />
    </motion.div>
  </r.switch.case>
</r.switch>

Render on the server

An async Server Component can fetch its data and render the list and its empty state, without turning into a client file.

export const Orders = async () => {
  const orders = await db.orders.findMany();

  return (
    <ul>
      <r.each
        items={orders}
        map={(order) => (
          <li key={order.id}>{order.number}</li>
        )}
        empty={<li>No orders yet</li>}
      />
    </ul>
  );
};

Change the tag, keep the types

Build a component on r.tag and its props follow the tag you choose: as="a" takes an href, as="div" rejects it.

type CardProps<T extends ElementType> =
  TagProps<T, { tone?: "light" | "dark" }>;

const Card = <T extends ElementType>({
  tone = "light",
  ...rest
}: CardProps<T>) => (
  <r.tag
    {...r.tag.forward<T>(rest, "article")}
    data-tone={tone}
  />
);

<Card as="a" href="/pricing" tone="dark">
  Pricing
</Card>

your turn

let's be declarative

start with one primitive, add the rest when you need them