# Quickstart

> Install Recipe Book, parse a recipe, and render it in your React app with <Recipe.Root> and the parts.

Source: https://recipe-book.siraj.page/quickstart/

Install one package and add two files to render a recipe in your React app, with a servings select that rescales its ingredients and a toggle that converts them between metric and US units.

1. Install the package:

   npm
   ```sh title="npm"
   npm i @recipe-book/react
   ```

   pnpm
   ```sh title="pnpm"
   pnpm add @recipe-book/react
   ```

   Yarn
   ```sh title="Yarn"
   yarn add @recipe-book/react
   ```

   Bun
   ```sh title="Bun"
   bun add @recipe-book/react
   ```

   With TypeScript, `strictNullChecks` must be on, or `if (!result.success)` in step 3 doesn't narrow the result and `result.error` fails with TS2339. `strict` includes it, and TypeScript 6.0 and later turn `strict` on by default. Use version 5.4 or newer, because on 5.0–5.3, emitting declarations (`declaration: true`) for your own exports whose inferred types come from Recipe Book can fail with TS2742.

   An app without React installs `@recipe-book/core` instead, as [Use core without React](https://recipe-book.siraj.page/introduction.md#use-core-without-react) describes. If a React app also installs `@recipe-book/core`, keep it on the same version as `@recipe-book/react`, or the app gets a second copy of core.

2. Save the sample recipe input as `recipe.ts`:

   recipe.ts
   ```ts title="recipe.ts"
   import type { RecipeInput } from '@recipe-book/react/core'

   export const pancakes = {
     name: 'Buttermilk pancakes',
     servings: 4,
     times: { prep: 10, cook: 15 },
     ingredients: [
       { name: 'all-purpose flour', quantity: '1 ½ cups', equivalents: ['190 g'] },
       { name: 'sugar', quantity: '2 tbsp', equivalents: ['25 g'] },
       { name: 'baking powder', quantity: '2 tsp' },
       { name: 'buttermilk', quantity: '1 ¼ cups' },
       { name: 'egg', plural: 'eggs', quantity: 2 },
       {
         name: 'butter',
         quantity: '3 tbsp',
         note: 'melted',
         equivalents: ['42 g'],
       },
       { name: 'maple syrup', note: 'to serve' },
     ],
     steps: [
       'Whisk the dry ingredients in a large bowl.',
       'Whisk the buttermilk, eggs, and butter, then fold into the dry ingredients.',
       'Cook ¼-cup portions on a buttered griddle, 2–3 minutes per side.',
     ],
   } satisfies RecipeInput
   ```

3. Parse the recipe and render it:

   pancakes.tsx
   ```tsx title="pancakes.tsx"
   import { parseRecipe } from '@recipe-book/react/core'
   import * as Recipe from '@recipe-book/react'
   import { pancakes } from './recipe'

   const result = parseRecipe(pancakes)
   if (!result.success) throw result.error
   const recipe = result.data

   export function Pancakes() {
     return (
       <Recipe.Root recipe={recipe}>
         <h1>{recipe.name}</h1>
         <Recipe.ServingsSelect />
         <Recipe.SystemToggle />
         <h2>Ingredients</h2>
         <Recipe.Ingredients />
         <h2>Steps</h2>
         <Recipe.Steps />
       </Recipe.Root>
     )
   }
   ```

Render `<Pancakes />` on any page of your app, including a Next.js Server Component. It shows the recipe at 4 servings in US units, as written. Choose **8 servings**, and the flour changes from 1 ½ cups to 3 cups. Then click **Metric**, and it changes to 380 g.

[`parseRecipe`](https://recipe-book.siraj.page/reference/core/parsing.md#parserecipe) checks the recipe input and returns the parsed recipe as `result.data`. [`<Recipe.Root>`](https://recipe-book.siraj.page/reference/react/root.md) holds the recipe's servings and measurement system, and the parts inside it read and set them: [`<Recipe.ServingsSelect>`](https://recipe-book.siraj.page/reference/react/servings.md#servingsselect), [`<Recipe.SystemToggle>`](https://recipe-book.siraj.page/reference/react/system-toggle.md), [`<Recipe.Ingredients>`](https://recipe-book.siraj.page/reference/react/ingredients.md), and [`<Recipe.Steps>`](https://recipe-book.siraj.page/reference/react/steps.md). The parts ship no CSS.

The sample's steps have no temperatures, so the toggle leaves them as written. In a recipe whose steps have temperatures, it converts them too, as [`<Recipe.StepText>`](https://recipe-book.siraj.page/reference/react/steps.md#steptext) describes.

## Runnable examples

Each project in `examples/` uses the same sample recipe:

- `nextjs-app-router`: a Next.js App Router page that parses the recipe in a Server Component and renders it with the parts, including its image through Next.js's `Image`, and its schema.org JSON-LD with [`<Recipe.JsonLd>`](https://recipe-book.siraj.page/reference/react/json-ld.md)
- `vite-spa`: a Vite single-page app that parses the recipe input in the browser, passes `result.data` to `<Recipe.Root>`, and styles the parts with plain CSS

  [Open in StackBlitz on the web page](https://recipe-book.siraj.page/quickstart/#runnable-examples)
- `node-script`: a Node script that parses the recipe and prints its ingredients, with no React and no build step

  [Open in StackBlitz on the web page](https://recipe-book.siraj.page/quickstart/#runnable-examples)
- `seo-json-ld`: an Astro page that renders the recipe at build time, writes its schema.org JSON-LD with [`<Recipe.JsonLd>`](https://recipe-book.siraj.page/reference/react/json-ld.md), and ships no client-side JavaScript

  [Open in StackBlitz on the web page](https://recipe-book.siraj.page/quickstart/#runnable-examples)
- `react-router-ssr`: a React Router app in framework mode that parses the recipe in a route's `loader` on the server, renders it with the parts and its schema.org JSON-LD with [`<Recipe.JsonLd>`](https://recipe-book.siraj.page/reference/react/json-ld.md), and hydrates the page in the browser
- `astro-island`: an Astro page that parses the recipe at build time, renders its image and its schema.org JSON-LD with no client-side JavaScript, and hydrates a React island with the servings and units controls, the ingredients, and the steps

To run `vite-spa`, `node-script`, or `seo-json-ld` in your browser, choose **Open in StackBlitz** under it. To run any example on your machine, use Node.js 22.23.4 or later. Copy the example's folder, delete the dependencies whose version is `workspace:*` from its `package.json`, and install `@recipe-book/react` with npm, or `@recipe-book/core` for `node-script`. Then run `npm run dev`, or `node src/index.ts` for `node-script`.

## Next steps

- [Render a recipe in React](https://recipe-book.siraj.page/guides/render-a-recipe-in-react.md) to keep the parser on the server, write components that read the recipe's state, and parse in the browser
- [Style a recipe](https://recipe-book.siraj.page/guides/style-a-recipe.md) to style the parts with `className`, `style`, and the data attributes they render
- [Author recipes](https://recipe-book.siraj.page/guides/author-recipes.md) to write your own recipe input
