user3162553 user3162553 - 4 months ago 47
React JSX Question

Redux Thunk Action Creator Not Getting Called

I have a function within

actions/index
that calls an action creator that dispatches an action but it never gets called. I think I'm missing something in Redux. The DB is a Firebase instance.

export function handleInitialVisit() {
...
DB.child('profiles')
.child(id)
.set(data)
.then(res => handleVisit());
}

function handleVisit() {
console.log('called') // called
return (dispatch, getState) => {
console.log('called'); // never called
...
dispatch({type: SET_DB_REF, payload: visitReferencePath});
}
}


My only guess is that because I'm not calling this with connect or the store it doesn't have those properties of dispatch and getState. Problem is, I don't need this to be called from a component. I want this to happen before a component is rendered. It is trying to figure out the user before interactions happen and components are rendered.

Answer

You should use dispatch function to dispatch actions:

export function handleInitialVisit() {
  ...
  DB.child('profiles')
    .child(id)
    .set(data)
    .then(res => dispatch(handleVisit()));
}