StoicBuilder

HandbookI the LudusCallbacks

Ithe Ludus, Read code

Callbacks

1 min readUpdated 28 November

In plain words

A callback is a function you pass to other code so it can call it later, like when a button is clicked or data arrives.

Where you see it

Every onClick in your React components takes a callback. In Quill, onClick={() => deleteNote(note.id)} hands the button a small function to run when Maya clicks, and not a moment before.

src/components/NoteCard.tsxbrowser
// runs on click: a function is passed<button onClick={() => deleteNote(note.id)}>Delete</button> // runs on every render: the result is passed<button onClick={deleteNote(note.id)}>Delete</button>
The () => wraps the call, so it waits for the click.

What goes wrong

  1. Calling the function instead of passing it

    onClick={deleteNote(note.id)} runs deleteNote the moment the page draws, so notes vanish on load. Wrap it: () => deleteNote(note.id).

    onClick={deleteNote(note.id)}
  2. Callbacks nested five deep

    Older-style code that tucks one callback inside another is hard to follow and easy to break. async/await does the same work and reads top to bottom.

  3. Expecting a callback to run straight away

    setTimeout and event listeners run their callback later. The next line runs first, so a value set inside the callback isn't there yet.

What to tell your agent

Copy this into Lovable, Bolt, Cursor or any agent
Check that every onClick, onChange and onSubmit in this component passes a function rather than calling one. Where callbacks are nested more than two deep, rewrite them with async/await and explain the order things now run in.

Quick check

Sign in to earn +10 XP for it

In Quill, every note disappears as soon as the notes page loads. The delete button says onClick={deleteNote(note.id)}. Why?

Got a prompt that touches callbacks?

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

Check your own prompt

Free, no sign-up.