等待React类组件方法中的redux-saga完成

很抱歉,如果这是一个重复的问题,但是我已经搜索了这个问题,但没有找到可以完全解决我问题的答案。我想在sagas中获取数据并在获取数据后更新localStorage。然后,我想在组件方法中this.forceUpdate()。但是显然this.forceUpdate()函数在数据之前启动 已加载。我可以将我的handleSubmit类方法包装到promise或async / await中,以使我的this.forceUpdate()等待数据被提取吗?谢谢。

FETCHING FUNCTION

export const fetchAuth = data => {
    return axios({
        method: "post",url: "http://url/",headers: {},data: {
            email: data.email,password: data.password.toString()
        }
    }).then(response => {
        return response.data;
    });
};

REDUX-SAGA

export function* authRequestFlow(action) {
    try {
        const tokens = yield call(fetchAuth,action.payload);
        if (tokens) {
            yield call(save,"access",tokens.access);
            yield call(save,"refresh",tokens.refresh);
            yield call(save,"isAuthorized",true);
            yield put(authSuccess());
        }
    } catch (error) {
        yield put(authFailure(error.message));
    }
}

COMPONENT

class Login extends PureComponent {
    handleSubmit = () => {
        authRequest();
        this.forceUpdate();
    };

    render() {
        if (localStorage.getItem("isAuthorized") === "true")
            return <Redirect to="/products" />;
        return (
            <div classname={styles.root}>
                    <Button
                        onClick={this.handleSubmit}
                    >
                        Submit
                    </Button>
            </div>
        );
    }
}
heaventoyou 回答:等待React类组件方法中的redux-saga完成

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3137807.html

大家都在问