简体   繁体   English

redux saga操作如何延迟存储数据更新?

[英]How delay redux saga action to store data update?

Can't delay action firing (initialize from redux-form) to store data update after fetching. 无法延迟操作触发(从redux-form初始化)来存储获取后的数据更新。

  1. Store is initializing with empty account object. 商店正在使用空帐户对象进行初始化。
  2. At initial render getAccount action firing and triggering update of store. 初始呈现时,getAccount操作将触发并触发商店更新。
  3. useEffect see store updating and triggering getAccount action second time useEffect查看商店更新并第二次触发getAccount操作
  4. second data request 第二个数据请求
  5. END 结束
  const {getAccount, initialize} = props
  prepareData = () => {...prepared obj}

  useEffect(() => {
    const begin = async () => {
      await getAccount();
      await initialize(prepareData());
    };
    begin();
  }, [account.id]);

main aim to avoid unnecessary second request 主要目的是避免不必要的第二次请求

What if you put a conditional on the begin call? 如果您将条件作为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]);

Should getAccount be called if account.id doesn't exist? 如果account.id不存在,应该调用getAccount吗? If not then simply check that account.id exists before calling begin. 如果没有,那么只需在调用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]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM