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

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

via Dev.to ReactUsama

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 . purpos

Continue reading on Dev.to React

Opens in a new tab

Read Full Article
2 views

Related Articles