Menu

1 / 2

1 RN Thing a Day – Day 13: Redux Thunk

DEV Community·Ola Abaza·6 months ago
#mtsxk3UY
Reading 0:00
15s threshold

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:

{ type : " SET_USER " , payload : user }

Enter fullscreen mode

Exit fullscreen mode

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

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

Enter fullscreen mode

Exit fullscreen mode

But with thunk, you can return a function:

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

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.