StoicBuilder

HandbookI the LudusDestructuring and spread

Ithe Ludus, Read code

Destructuring and spread

1 min readUpdated 28 November

In plain words

Destructuring pulls named values out of an object or array in one line, and spread, the three dots, copies everything from one into another.

Where you see it

Nearly every Supabase call your agent writes begins with const { data, error }. That's destructuring: it takes two keys from the response and gives each a name. The three dots in setNote({ ...note, title }) are spread, copying the old note and replacing one field.

src/components/NoteEditor.tsxbrowser
// destructuring: take two keys out of the responseconst { data, error } = await supabase.from("notes").select("*"); // spread: copy every field, then change oneconst updated = { ...note, title: "Groceries" }; // order matters: the old title wins hereconst oops = { title: "Groceries", ...note };
Whatever comes later in the braces overwrites what came before.

What goes wrong

  1. Spreading in the wrong order

    { title: newTitle, ...note } puts the old title back on top of the new one, so the edit never sticks. Spread first, changes after.

  2. Destructuring something that isn't there yet

    const { name } = member throws "Cannot destructure property 'name'" when member is undefined. Wait for the data, or fall back to an empty object.

    const { name } = member ?? {};
  3. Reading a rename as something else

    In const { data: notes } = ..., the colon renames data to notes. It isn't a type, and there's no variable called data afterwards.

What to tell your agent

Copy this into Lovable, Bolt, Cursor or any agent
When updating an object in state, spread the old value first and put the changed fields after it. Add a short comment beside any destructuring that renames a key, so I know where each name comes from.

Quick check

Sign in to earn +10 XP for it

Maya renames a Quill note, but the old title comes back after saving. The code builds { title: newTitle, ...note }. Why?

Got a prompt that touches destructuring and spread?

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

Check your own prompt

Free, no sign-up.