Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

开发模式下 两次调用 useEffect warning的原因。 #828

Open
JacopoWW opened this issue Aug 6, 2024 · 0 comments
Open

开发模式下 两次调用 useEffect warning的原因。 #828

JacopoWW opened this issue Aug 6, 2024 · 0 comments

Comments

@JacopoWW
Copy link

JacopoWW commented Aug 6, 2024

dispatch({ type: TypingStateActionType.REPORT_WRONG_WORD, payload: { letterMistake: currentState.letterMistake } })

这里的dispath在setState回调函数中才被调用,导致在dispatch后,组件已经被销毁。

解决方案,直接在useEffect内调用dispatch

} else {
      // 出错时
      playBeepSound()
      const letterMistake = structuredClone(wordState.letterMistake )
      if (letterMistake[inputLength - 1]) {
        letterMistake[inputLength - 1].push(inputChar)
      } else {
        letterMistake[inputLength - 1] = [inputChar]
      }
      dispatch({ type: TypingStateActionType.REPORT_WRONG_WORD, payload: { letterMistake } })
      setWordState((state) => {
        state.letterStates[inputLength - 1] = 'wrong'
        state.hasWrong = true
        state.hasMadeInputWrong = true
        state.wrongCount += 1
        state.letterTimeArray = []
      })

      if (currentChapter === 0 && state.chapterData.index === 0 && wordState.wrongCount >= 3) {
        setShowTipAlert(true)
      }
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant