【发布时间】:2017-08-15 14:55:39
【问题描述】:
我的操作中有以下功能
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) 函数,但它可以在其他方面工作。导入和依赖不是问题,因为我已经验证了很多次。
任何建议、提示或解决方案都会有很大帮助。如果您需要任何关于我的问题的说明或上下文,请告诉我。
提前致谢。
【问题讨论】:
-
你能分享一下你的
connect你的组件吗? -
在编辑中包含了一些额外的细节。请看一看。
-
看起来你应该调用 this.props.fetchDeviceData(...)
-
不工作,给我一个 TypeError: this.props.fetchDeviceData is not a function。如果我从道具调用它,我需要在任何地方传递这个函数吗?对不起,仍然是新的反应。实际上这是 Header 组件的一部分,只需要调用一次,所以我没有为它制作路由。当我记录 this.props 时,它给了我一个空对象。
标签: reactjs react-redux redux-thunk