scrimba
Learn React - Side Effects (Section 4)
Sneak peek: refs
Go Pro!Bootcamp

Bootcamp

Study group

Collaborate with peers in your dedicated #study-group channel.

Code reviews

Submit projects for review using the /review command in your #code-reviews channel

Sneak peek: refs
AboutCommentsNotes
Sneak peek: refs
Expand for more info
components
Main.jsx
run
preview
console
import React from "react"
import IngredientsList from "./IngredientsList"
import ClaudeRecipe from "./ClaudeRecipe"
import { getRecipeFromChefClaude, getRecipeFromMistral } from "../ai"

export default function Main() {
const [ingredients, setIngredients] = React.useState(
["chicken", "all the main spices", "corn", "heavy cream", "pasta"]
)
const [recipe, setRecipe] = React.useState("")

async function getRecipe() {
const recipeMarkdown = await getRecipeFromChefClaude(ingredients)
setRecipe(recipeMarkdown)
}

function addIngredient(formData) {
const newIngredient = formData.get("ingredient")
setIngredients(prevIngredients => [...prevIngredients, newIngredient])
}

return (
<main>
<form action={addIngredient} className="add-ingredient-form">
<input
type="text"
placeholder="e.g. oregano"
aria-label="Add ingredient"
name="ingredient"
/>
<button>Add ingredient</button>
</form>

{ingredients.length > 0 &&
<IngredientsList
ingredients={ingredients}
getRecipe={getRecipe}
/>
}

{recipe && <ClaudeRecipe recipe={recipe} />}
</main>
)
}
Console
/
-9:54