How-to

Website

Web app

Pass a store's value to a component

A component that needs only one value from a store can take it as a prop instead of using the store. That keeps the component reusable outside that store: a test or another page can hand it any number:

Needs: nothing besides the code. It claims /: paste it into a new project, or replace the starter's src/pages/Home.wf with it.

wf

store Cart { state count = 0 } component CartBadge(count: Number) { if count > 0 { Badge("{count}").primary } } page Shop(path: "/", title: "Shop", description: "Everything we sell.") { use Cart Heading("Shop").h1 Row(gap: .sm, align: .center) { Button("Add to cart") { on click { Cart.count = Cart.count + 1 } } CartBadge(count: Cart.count) } }

How it works

No badge shows while the cart is empty. Press Add to cart and a badge reading 1 appears beside the button, and counts up with every press.

  • CartBadge declares a count prop and knows nothing about Cart.
  • The page reads the store after use Cart and passes Cart.count down.
  • A prop that reads state stays live inside the component, so the badge repaints when Cart.count changes. The button changes it here; in a shop of your own, an action of the store would.