🔧 AI Nachrichten Hands-On with ChatGPT Work’s New Cloud Browser Feature(26.08.2026 um 17:06 Uhr)
🔧 AI Nachrichten Anthropic Introduces an In-App Browser for Claude Cowork(27.08.2026 um 16:18 Uhr)
🔧 AI Nachrichten Monthly Log: August 2026(31.08.2026 um 16:24 Uhr)
🔧 AI Nachrichten Inside OpenAI’s Codex with Andrew Ambrosino(31.08.2026 um 17:26 Uhr)
🎥 PodcastsDesigned in California Makes Its Official Debut(03.09.2026 um 17:59 Uhr)
🔧 AI Nachrichten Major AI platforms go down in unprecedented simultaneous outage(03.09.2026 um 17:34 Uhr)
🔧 AI Nachrichten Trump Administration Sides With OpenAI In New York Times Lawsuit(03.09.2026 um 05:43 Uhr)
🔧 AI Nachrichten Government Filing Argues AI Innovation Requires Copyright Theft(03.09.2026 um 11:42 Uhr)
🔧 AI Nachrichten Hands-On with ChatGPT Work’s New Cloud Browser Feature(26.08.2026 um 17:06 Uhr)
🔧 AI Nachrichten Anthropic Introduces an In-App Browser for Claude Cowork(27.08.2026 um 16:18 Uhr)
🔧 AI Nachrichten Monthly Log: August 2026(31.08.2026 um 16:24 Uhr)
🔧 AI Nachrichten Inside OpenAI’s Codex with Andrew Ambrosino(31.08.2026 um 17:26 Uhr)
🎥 PodcastsDesigned in California Makes Its Official Debut(03.09.2026 um 17:59 Uhr)
🔧 AI Nachrichten Major AI platforms go down in unprecedented simultaneous outage(03.09.2026 um 17:34 Uhr)
🔧 AI Nachrichten Trump Administration Sides With OpenAI In New York Times Lawsuit(03.09.2026 um 05:43 Uhr)
🔧 AI Nachrichten Government Filing Argues AI Innovation Requires Copyright Theft(03.09.2026 um 11:42 Uhr)

🔧 Programmierung 🕛 kürzlich 3 Min Lesezeit
0

1 RN Thing a Day – Day 13: Redux Thunk

↗ Quelle (dev.to)
🗣️ Stimme:

What Is Redux Thunk?

Redux Thunk is a middleware for Redux that allows you to write action creators that return a function instead of a plain action object.





Normally, Redux actions look like this:




CODE
{
type: "SET_USER",
payload: user
}






async code doesn't work directly: Redux would throw an error because it expects an object.




CODE
dispatch(async () => { 
const data = await api.getUser()
})






But with thunk, you can return a function:




CODE
const fetchUser = () => {// Return an async function that receives dispatch as parameter
return async (dispatch) => {
const response = await api.getUser()

dispatch({
type: "SET_USER",
payload: response.data
})
}
}






Thunk acts as the bridge between async operations and Redux state updates.



The Problem Without Thunk

Imagine handling login directly inside a screen:




CODE
const handleLogin = async () => {
setLoading(true)

try {
const response = await api.login(email, password)

dispatch({
type: "LOGIN_SUCCESS",
payload: response.data
})
} catch (error) {
setError(error.message)
}

setLoading(false)
}






This approach causes problems:




  • Components become bloated

  • Logic is duplicated

  • Reusability decreases

  • Testing becomes harder

  • UI and business logic become tightly coupled



The Same Logic Using Thunk:




CODE
export const loginUser = (email, password) => {
return async (dispatch) => {
dispatch(setLoading(true))

try {
const response = await api.login(email, password)

dispatch(loginSuccess(response.data))
} catch (error) {
dispatch(loginError(error.message))
} finally {
dispatch(setLoading(false))
}
}
}






Then inside the component:




CODE
dispatch(loginUser(email, password))







Now the component becomes clean and focused only on UI.






Redux Toolkit createAsyncThunk



Modern Redux apps should prefer Redux Toolkit. Instead of manually writing thunk boilerplate.



The Structure Usually:




  • Service Layer: Handles API only.




CODE
// services/userService.js

export const getProducts = async () => {
const response = await fetch(API_URL)

return response.json()
}







  • Thunk Layer: Handles async Redux logic.




CODE
// store/thunks/userThunk.js

export const fetchProducts = createAsyncThunk(
"products/fetchProducts", // action type prefix. // sliceName/actionName

async () => {
return await getProducts()
}
)






"products/fetchProducts" It is simply:a unique Redux action identifier prefix used to generate async action types automatically.




  • Slice Layer: Handles state updates.




CODE
import { createSlice } from "@reduxjs/toolkit"
import { fetchProducts } from "./productThunk"

const productSlice = createSlice({
name: "products",

initialState: {
products: [],
loading: false,
error: null
},

reducers: {},

extraReducers: (builder) => {
builder
.addCase(fetchProducts.pending, (state) => { /
state.loading = true
})

.addCase(fetchProducts.fulfilled, (state, action) => {
//internally matches:products/fetchProducts/fulfilled
state.loading = false
state.products = action.payload
})

.addCase(fetchProducts.rejected, (state, action) => {
state.loading = false
state.error = action.error.message
})
}
})

export default productSlice.reducer







Benefits:




  • Cleaner syntax

  • Built-in pending/fulfilled/rejected states

  • Less boilerplate

  • Better TypeScript support




Important Concept

createAsyncThunk is NOT mainly about API calls.

It is about:

Managing async operations that affect global state. API calls are just the most common example.




Better understanding:

Thunk = async business logic connected to Redux state



That business logic may include:




  • API calls

  • caching

  • conditional fetching

  • retries

  • authentication

  • reading current state

  • dispatching multiple actions

Vollständiger Original-Bericht
Ausführliche Details, Code-Beispiele & Hersteller-Stellungnahme auf dev.to.
↗ Original-Artikel auf dev.to lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 43%
🟡 In Evaluierung 24%
🟢 Keine Auswirkung 13%
Spannende Innovation 20%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
1 Quelle
ChatGPT showing blank screen [Fix]
1 Quelle
Sofort deinstallieren: Diese 19 Browser-Erweiterungen sind mit Malware verseucht
1 Quelle
Creator Panel – One Creator, Full Production: Der neue Creator Workflow
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten 1 RN Thing a Day – Day 13: Redux Thunk

Thematisch verwandte Begriffe: Thing, Redux, Thunk · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...