【问题标题】:the same action when dispatched for the second time does not call the saga, while it does correctly in the first tie第二次调度的相同动作不会调用 saga,而在第一次调度时它会正确调用
【发布时间】:2020-04-24 21:57:29
【问题描述】:

在第一次加载后调用 saga 时,它可以正常工作。 但是当在 saga 上执行两次相同的操作时,该操作被调度,但 saga 从未收到它。

useEffect(() => {
		if (show) {
			updateTitle(message.title);
			if (!status || status === Status.Initiate) {
				createStatus();
			}
		}
		return () => {
			if (!show) {
				cleanup();
			}
		};

	}, [status]);

【问题讨论】:

  • 如果第一次调用后status变量没有变化,那么作为它的一个依赖,useEffect不会被触发。见here
  • updateTitle() 做什么?它会更新该州的哪个部分?
  • @BenSmith useEffect 正在被触发,实际上我可以看到在 redux 中调用了动作函数,除了在调度动作后没有任何事情发生。 dispatch 和 saga 之间缺少通信。 @wentjun updateTitle 更新状态的一些不同部分。
  • 我建议将您的 saga 和 reducer 代码添加到问题中。

标签: reactjs typescript react-redux react-hooks redux-saga


【解决方案1】:

以下过程解决了我的问题。

事实证明,在运行时,如果某个 saga 无法执行,则该 saga 将不再被调用,这意味着如果为该 saga 生成任何操作,它就会被忽略。

要检查 saga 在运行时是否失败,您可以将 saga 的主体包装在 try catch 块中。

function* failingSaga() {
  try {
  
    // saga-body
  } catch(e) {
    console.log(e);
  }
}

这将有助于了解异常是什么。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-31
    • 2016-10-03
    • 2020-08-19
    • 1970-01-01
    • 2017-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多