StoicBuilder

HandbookII the ForumState

IIthe Forum, The shape of an app

State

1 min readUpdated 28 November

In plain words

State is the data a screen keeps in memory, like the items in a cart or whether a menu is showing, and changing it makes the screen redraw.

Where you see it

You tap + on a QuickBite dish and the quantity goes from 1 to 2 while the total updates. Nothing was saved anywhere: that number lives in state, which is why it's back to 1 after a refresh.

components/DishRow.tsxbrowser
const [quantity, setQuantity] = useState(1);const total = price * quantity; return ( <button onClick={() => setQuantity(quantity + 1)}> Add another: {quantity} for {total} </button>);
setQuantity changes the number, and React redraws the button with the new total.

What goes wrong

  1. Changing a list in place

    React redraws when state gets a new value. Pushing into the existing array changes the data, but React doesn't notice and the cart badge stays at 2.

    cart.push(dish); setCart(cart);
  2. Expecting state to survive a refresh

    State lives in the open tab's memory. Anything that must outlast a refresh, or show up on another device, belongs in the database.

  3. The same data kept in two places

    Keep the cart in one component's state and a copy in another, and they drift apart: the badge says 3, checkout says 2. Keep one source and pass it down.

What to tell your agent

Copy this into Lovable, Bolt, Cursor or any agent
For this screen, list every piece of state, where it lives and whether it survives a refresh. Keep one source of truth for each value, and update lists and objects by making new copies rather than changing them in place.

Quick check

Sign in to earn +10 XP for it

Maya adds a note in Quill, refreshes the page, and the note is gone. What happened to it?

Got a prompt that touches state?

Paste it. You get a better prompt first, then the cards that explain it.

Check your own prompt

Free, no sign-up.