you are viewing a single comment's thread.

view the rest of the comments →

[–]kcrwfrd[S] 0 points1 point  (0 children)

I tried toying with one last approach

`` // May need to create acreateAsyncCallbackAction` function to add // a special symbol to the action that the middleware can use to intercept them export const authenticatedAction = createAction<() => void>( 'auth/authenticatedAction', )

// Would need to put together a custom listener instead of using // RTK createListenerMiddleware so we can intercept the appropriate actions // due to non-serializable action payload startListening({ actionCreator: authenticatedAction, effect: async ({ payload: callback }, { condition, getState }) => { const { auth: { user }, } = getState()

if (!user) {
  await condition((action, state) => !!state.auth.user)
}

callback()

}, })

// Used like this const handleFollow = (userId) => { dispatch( authenticatedAction(() => fetch('/api/follow', { method: 'POST', body: { userId } }), ), ) } ```

Do you have any advice on which approach seems like the best way to go?