Search code examples
reactjsreduxredux-thunk

Is it okay to pass dispatch and getState from a Redux thunk function down into helper functions?


Let's say I have a thunk which is a top-level function:

export default () => (dispatch, getState) => {
    // do stuff here

    childFunction(getState) // is it okay to pass in getState so that children down the chain can call getState?
    anotherChildFunction(dispatch) // is it okay to pass in dispatch so that children down the chain can dispatch something?
}

In the above example, let's say that both childFunction and anotherChildFunction don't directly use their arguments but pass it on further down to their child functions and to their child functions and eventually one of them needs it to dispatch an action or retrieve something from the Redux state.

Is it okay to pass these down the chain or is there another way that I'm not thinking about?


Solution

  • Yes, that's totally fine. Alternately, you could define those other functions as thunks and dispatch them as well, but if they're only ever used as "helper functions" just passing in dispatch or getState directly is very reasonable.