说真的,我正在绝望地寻找问题所在。这几天找不到答案!
React.StrictMode
API 导致我们setState
被调用两次,对吧?如果它产生错误,则意味着我们的setState
回调中的某个地方是不纯的。那么是哪一个呢?
setOrganization((initialValue) => {
const newOrganization = { ...initialValue };
const oldIssues = [...newOrganization.repository.issues.edges];
const newIssues = [...data.data.organization.repository.issues.edges];
newOrganization.repository.issues.edges = [...newIssues, ...oldIssues];
return newOrganization;
});
在第一次调用时,
oldIssues
返回预期值,例如[{id: issue1}, {id: issue2}]
.newIssues
值例如[{id: issue3}]
但是在第二次调用时,
oldIssues
奇怪地变成了oldIssues
and的组合newIssues
。(第二次通话,oldIssues
已经[{id: issue1}, {id: issue2}, {id: issue3}]
)。使第二个
newOrganization.repository.issues.edges
值加倍newIssues
。[{id: issue1}, {id: issue2}, {id: issue3}, {id: issue3}]
完整的脚本可以在这里找到,第 101 行:https ://pastebin.com/ugsrBRTM