在redux中,我有一个(count,remote count(我将两者默认设置为0!
主要想法是比较计数是否等于远程计数我想调度一个锁定应用的操作"真/假">
在主屏幕上从API获取远程计数,然后将其保存在redux存储中,它保存得很好
但我有一个if语句,它检查是否计数==远程计数我锁定了应用程序
所以这个语句在我保存远程计数之前调用,我想尽管我在((中添加了它
主屏幕
getRemoteCount = async () => {
try {
let response = await API.get('/number/subsribtion');
let remoteCount = response.data.number;
this.props.saveRemoteCount(remoteCount); // it's saved the remote count!
} catch (err) {
console.log(err);
}
};
componentDidMount() {
const {remoteCount, count} = this.props;
this.getRemoteCount().then(() => {
if (count == remoteCount) {
console.log('count', count);
console.log('remoteCount', remoteCount);//it's log 0!! so the next line invoke!
this.props.isAppLock(true);
}
});
}
使用render获取更新的计数。componentDidMount在首次装载组件时运行。保存组件中redux store
和mapToState
的计数。
class C {
getRemoteCount = async () => {
try {
let response = await API.get("/number/subsribtion");
let remoteCount = response.data.number;
this.props.saveRemoteCount(remoteCount); // it's saved the remote count!
} catch (err) {
console.log(err);
}
};
componentDidMount() {
this.getRemoteCount();
}
render() {
const { remoteCount, count } = this.props;
if (count == remoteCount) {
console.log("count", count);
console.log("remoteCount", remoteCount); //it's log 0!! so the next line invoke!
this.props.isAppLock(true);
}
}
}
您可以使用redux thunk使用dispatch async
函数。
-
安装redux thunk。
-
用作商店内部的中间件。
import thunk from 'redux-thunk';
import rootReducer from './reducers';
const store = createStore(rootReducer, applyMiddleware(thunk));
- 将其用于调度异步操作
return async dispatch=>{ let res= await authLogin(data)}