Fullstack CourseLearn by building
Back to week 2

Topic

RTK Essentials practice: client drafts

Definition

A Redux Toolkit slice can hold a client-only draft that survives navigation until the user submits or discards it; after Nest accepts the write, reset the draft and invalidate the Query cache.

In simpler words

Practice the happy path: type in RTK, save with Nest, then clear the draft and refresh the list.

This is our Redux Essentials-style lab: use RTK for a composer draft after Quick Start, then ship it in Week 4 Notes projects.

After this you can

  • Model a multi-step draft slice with reset
  • Survive route changes without losing composer text
  • Submit via mutation then reset + invalidate
  • Avoid mirroring Nest entities into the draft slice

Draft lifecycle (simple story)

While you type, Nest does not know the draft yet — RTK remembers it.

When create succeeds, Nest owns the new row. Clear the RTK draft and tell TanStack Query to refetch the list.

If create fails, keep the draft so the user can fix and retry.

Submit boundary

const draft = useAppSelector((s) => s.notesDraft);
const mutation = useMutation({
  mutationFn: () => notesApi.create({
    title: draft.title,
    body: draft.body,
  }),
  onSuccess: async () => {
    dispatch(notesDraftActions.reset());
    await queryClient.invalidateQueries({ queryKey: noteKeys.lists() });
  },
});

Same pattern you will implement in Week 4 Notes UI projects.

Mistake: never clear after success

await create(draft);
// draft still full — composer looks dirty forever

Reset only after Nest confirms.

Where bugs hide

Do not sync draft ↔ Nest entity with an Effect on every prop change — remount with key={id} or initialize once.

Do not store JWTs in RTK; cookies stay httpOnly.

Mistake: Effect sync draft ↔ entity

// Wrong
useEffect(() => setDraft(note), [note]);

// Right
<Editor key={note.id} />

Avoid ping-pong sync Effects.

Live playground

Draft sandbox

Edit step/title, then reset like a successful Nest create.

Mini createSlice feel: dispatch named actions → state updates → UI re-renders.

value=0 · title="(empty)"

createSlice → configureStore → Provider → dispatch(action)

Keep in mind

  • Official next read: Redux Essentials on redux.js.org (Tutorials Overview).
  • Draft in RTK; confirmed rows in Query.
  • Ship the full flow in Week 4 Notes UI projects.

Test

Check your understanding

At least 10 questions — mix of concept, syntax, practical, and logic. Score ≥ 80% (enforced by the API) to save progress.

Checking your session…

10 questions · concept 2 · syntax 2 · practical 3 · logic 3

Concept1. What is an RTK client draft?
Practical2. After Nest successfully creates a ticket from a draft, what should you do?
Logic3. Why not render draft rows inside the Nest-backed list UI?
Syntax4. Which pattern submits a draft with Query while RTK holds the fields?
Practical5. On mutation failure, what should happen to the draft?
Logic6. Why is useEffect(() => setDraft(ticket), [ticket]) often a bug?
Concept7. Should JWTs live in an RTK slice in this academy?
Syntax8. How do you read the wizard title in a component?
Practical9. Where should you practice the draft → Nest → invalidate flow in this course?
Logic10. What does surviving client navigation imply for a draft slice?