获取后不能延迟动作触发(从redux形式初始化)以存储数据更新。
const {getAccount, initialize} = props
prepareData = () => {...prepared obj}
useEffect(() => {
const begin = async () => {
await getAccount();
await initialize(prepareData());
};
begin();
}, [account.id]);
主要目的是避免不必要的第二次请求
答案 0 :(得分:0)
如果您要在begin
通话中添加条件怎么办?
const {getAccount, initialize} = props
const {begun, setBegun} = useState(false)
prepareData = () => {...prepared obj}
useEffect(() => {
const begin = async () => {
await setBegun(true);
await getAccount();
await initialize(prepareData());
};
begun || begin();
}, [account.id]);
答案 1 :(得分:0)
如果getAccount
不存在,应该调用account.id
吗?如果不是,那么只需在调用begin之前检查account.id
是否存在。
const {getAccount, initialize} = props
prepareData = () => {...prepared obj}
useEffect(() => {
// add a guard condition to exit early if account.id doesn't exist yet
if (!account.id) {
return;
}
const begin = async () => {
await getAccount();
await initialize(prepareData());
};
begin();
}, [account.id]);