2016-08-05 31 views
1

我在actions/index中有一個函數,它調用一個動作創建器來調度一個動作,但它永遠不會被調用。我想我在Redux中錯過了一些東西。該數據庫是一個Firebase實例。Redux Thunk Action Creator Not Get Called

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}); 
    } 
} 

我唯一的猜測是,因爲我不是調用此與連接或商店它沒有調度和的getState的那些屬性。問題是,我不需要從組件調用它。我想在渲染組件之前發生這種情況。它試圖在交互發生和組件呈現之前找出用戶。

回答

1

您應該使用dispatch功能派遣行動:

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

有道理。但是,在組件之外調用redux動作創建器是不可能的(或不好的做法)?我希望在我的應用程序中呈現任何內容之前調用它。 – user3162553

+0

這取決於情況。你可以使用'store.dispatch(actionCreator())'來做到這一點。 – 1ven

+0

好,那很有道理。謝謝。但出口商店是不好的做法嗎? – user3162553