异步redux thunk需要包装函数

时间:2020-02-28 15:02:11

标签: react-native redux react-redux redux-thunk

我的redux thunk需要包装函数。 (我开玩笑的模拟商店不需要它) Thunk已添加为我的商店中的中间件

有效的操作:

export function getSettings (establishmentId: string) {
    return (dispatch: any) => {
        return () => {
            dispatch(settingsLoading())
            Client
                .getSettings(establishmentId)
                .then((settings: ISettings) => {
                    dispatch(getSettingsSuccess(settings))
                })
        }
    }
}

发货:

dispatch(getSettings(establishmentId))

无效的操作(但在我的笑话测试中有效):

export function getSettings (establishmentId: string) {
    console.log('GET SETTINGS')
    return (dispatch: any) => {
        dispatch(settingsLoading())
        return Client
            .getSettings(establishmentId)
            .then((settings: ISettings) => {
                // dispatch(getSettingsSuccess(settings))
            })
    }
}

在没有包装函数的情况下,它告诉我应该使用thunk中间件(我这样做了)

return createStore(
        rootReducer,
        composeEnhancers(
            applyMiddleware(thunk)
        )
    )

1 个答案:

答案 0 :(得分:1)

找到答案,我们在mapDispatchToProps中犯了一个错误,并结合使用bindActionCreators和仅返回对象列表:

request${i}

应该是:

const mapDispatchToProps = (dispatch: any) => bindActionCreators(
    {
        getSettings: (establishmentId: string) => dispatch(getSettings(establishmentId))
    },
    dispatch
)