很抱歉,如果这是一个重复的问题,但是我已经搜索了这个问题,但没有找到可以完全解决我问题的答案。我想在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>
);
}
}