0

I can't understand advantage of Redux Thunk in async requests.

....

const mapDispatchToProps = (dispatch:any)=>{
    return{
        onStoreData: (val:number)=> push(dispatch, val),
    };
};
export const push = (dispatch:any, val: number) => {
    setTimeout(()=>{
        dispatch(
            {
                type: Actions.PUSH,
                val: val
            }
        );
    }, 2000);
};

This code works without Redux Thunk even when there are many requests in the same moment. Is this code safe? What is point of Redux Thunk?

  • Answered in https://stackoverflow.com/q/35411423/6281832 – Rodrigo Amaral Jul 15 '20 at 20:53
  • So above code is fine, and Redux Thunk is just for cosmetic purposes and shorter code? – Rozrewolwerowany rewolwer Jul 15 '20 at 21:20
  • 1
    Your code works, but gets more complicated over time. Another good thing that redux-thunk provides access to the whole state via getState. Another question that explains the difference https://stackoverflow.com/q/34570758/6281832 Also, check the source code of redux-thunk, it's really tiny https://github.com/reduxjs/redux-thunk/blob/master/src/index.js – Rodrigo Amaral Jul 15 '20 at 21:38

0 Answers0