DEV Community

Cover image for I Finally Understood How to Pass Multiple Values in Redux Toolkit (The Right Way)
Usama
Usama

Posted on

I Finally Understood How to Pass Multiple Values in Redux Toolkit (The Right Way)

Today was one of those small learning moments…
but it completely changed how I write Redux logic.

I used to think:

👉 “Redux actions can only take one value”
👉 “If I need more data, things will get messy”

But today I learned a clean and scalable way to handle multiple values in Redux Toolkit using the prepare function.


The Problem I Faced

I was trying to dispatch an action like this:

  • Loan amount
  • Loan purpose

At first, I didn’t know how to pass both values properly.

I thought maybe I need:

  • multiple actions
  • or a complex payload structure

But that would make things messy and hard to maintain.


The Solution: prepare Function in Redux Toolkit

Then I discovered something powerful inside createSlice:

👉 The prepare method

Here’s the code I worked with:

requestLoan: {
  prepare(amount, purpose) {
    return {
      payload: { amount, purpose },
    };
  },

  reducer(state, action) {
    if (state.loan > 0) return;

    state.loan = action.payload.amount;
    state.loanPurpose = action.payload.purpose;
    state.balance += action.payload.amount;
  },
}
Enter fullscreen mode Exit fullscreen mode

What’s Happening Here?

This is what I understood:

1️⃣ prepare Handles Multiple Arguments

Instead of passing one object manually, I can do this:

dispatch(requestLoan(5000, "Buy Car"));
Enter fullscreen mode Exit fullscreen mode

And prepare converts it into:

payload: {
  amount: 5000,
  purpose: "Buy Car"
}
Enter fullscreen mode Exit fullscreen mode

Clean. Structured. Scalable.


2️⃣ Reducer Stays Simple

Inside the reducer, I don’t worry about arguments anymore.

I just use:

  • action.payload.amount
  • action.payload.purpose

Everything is already organized.


3️⃣ Logic Feels More Professional

This approach helped me:

  • avoid messy payload creation in components
  • keep logic centralized
  • write cleaner and more readable reducers

Why This Matters

Before this, I was thinking in a limited way.

Now I understand:

👉 Redux Toolkit is designed to simplify complex patterns
👉 You don’t need hacks — just the right tools

Using prepare:

  • makes actions flexible
  • keeps reducers clean
  • improves scalability

My Key Takeaway

Today I learned:

You don’t need multiple actions for multiple values.

You just need a better way to structure your payload.

And prepare gives you exactly that.


Final Thoughts

This wasn’t a big feature.

But it was a big mindset shift.

Redux Toolkit is not just about writing less code…

It’s about writing smarter and cleaner code.


If you're learning Redux Toolkit, don’t ignore small features like this.

Sometimes, these small things make the biggest difference.

Top comments (1)

Collapse
 
aaron_rose_0787cc8b4775a0 profile image
Aaron Rose

Great write-up, Usama! The prepare callback is one of those RTK features that flies under the radar until you actually need it — and then you wonder how you lived without it.

One thing worth knowing for when your slices grow: prepare also lets you normalize or validate data before it hits the reducer. So you can do things like coerce types, generate a UUID for an id field, or strip out values you don't want in state — all in one place before the reducer ever sees it.

Keep the learning posts coming. Cheers! ❤💯🌹