2017-08-15 52 views
0

我在行動以下功能無法調用咖喱功能從componentDidMount()的陣營組成

export const fetchDeviceData = (deviceData: Object): ThunkAction => (dispatch: Dispatch, getState: GetState, axios: any) => { 

    console.log('inside fetchDeviceData', deviceData); 
    return dispatch(fetchDeviceDataAPI(axios, url, deviceData)); 

}; 

,我把它叫做以下列方式在componentDidMount()塊在我的組件

// type declaration (imported from a separate file) 
export type DeviceReg = { 
    [deviceData: Object]: { 
     readyStatus: string, 
     err: any, 
     payload: Object, 
    } 
}; 

// Prop type declaration 
type Props = { 
    device_reg: DeviceRegType, 
    fetchDeviceData:() => void, 
} 

componentDidMount() { 
    const x = this.getDeviceData(); 
    console.log('header mount mobile', x); 
    action.fetchDeviceData(x); 
} 

// The connector 
const connector: Connector<{}, Props> = connect(
    ({ device_reg }: Reducer) => ({ device_reg }), 
    (dispatch: Dispatch) => ({ 
     fetchDeviceData: (deviceData) => dispatch(action.fetchDeviceData(deviceData)), 
    }), 
); 
export default Header; 

問題是,當我在函數中包含參數dispatch: Dispatch, getState: GetState, axios: any時,fetchDeviceData(x)函數沒有被調用,但它在其他方面起作用。導入和依賴關係不是問題,因爲我已經驗證了它們的加載次數。

任何建議,提示或解決方案將有很大的幫助。如果您需要關於我的問題的任何澄清或背景,請告訴我。

在此先感謝。

+0

你可以分享你如何'connect'你的組件axios說法? – thedude

+0

在編輯中包含了一些額外的細節。請看一下。 – Deepank

+0

看起來像你應該調用this.props.fetchDeviceData(...) – thedude

回答

1

您可以嘗試從dispatch: Dispatch, getState: GetState, axios: any中刪除axios: any

我認爲應該只有dispatch和getState,因爲這就是通過redux-thunk注入的東西。

+0

已經試過了,沒有工作。控制進入函數的唯一情況是當我刪除所有三個參數(dispatch,getState和axios)時。無法弄清楚原因。 – Deepank

0

試着改變你的連接代碼,使用對象的形式:

connect(
    <yourMapStateToProps>, 
    { 
     fetchDeviceData: actions.fetchDeviceData, 
    }  
) 

這應該使fetchDeviceData可作爲你的一個道具叫。

另外,儘量刪除函數簽名只留下dispatchgetState

+0

好吧,會嘗試。但你能告訴我爲什麼這種方法不起作用嗎?在組件中直接調用一個函數是不是正確的? – Deepank

+0

不,操作應該是'dispatch'ed(除非它們通過'connect'綁定,正如我上面所描述的那樣) – thedude