0

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?

noblerare
  • 10,277
  • 23
  • 78
  • 140
  • As long as your reducer remains pure and you don't mix async into a reducer function it should not matter. – OFRBG Nov 04 '22 at 01:59

1 Answers1

1

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.

markerikson
  • 63,178
  • 10
  • 141
  • 157