1

useReducer用来更新errorsState用户登录并失败的时间。我读过很多解决方案,据说dispatch是异步的,我知道,所以我把它console.log放进useEffect去看看errorsState变化,但不幸的是它没有改变。这是我的代码

登录.jsx

export default function Login({ userProps }) {
  //
  // some variables and state
  //
  const { loading, user } = useLogin({ email: state.email }, state.submitted)
  const [errors, dispatch] = useReducer(errorsReducer, errorsState)

  useEffect(() => {
    console.log("errors", errors) // it won't triggered because errors state didn't updating from UseLogin
  }, [errors])

  return content
}

这是获取功能useLogin

AuthAction.js

export const useLogin = (data, submitted) => {
  const [state, dispatch] = useReducer(userReducer, userState)
  const [errors, errorsDispatch] = useReducer(errorsReducer, errorsState)

  useEffect(() => {
    if (!submitted) return

    dispatch({
      type: USER_ACTIONS.MAKE_REQUEST,
    })

    ticketApi.login(data).then(({ res, status }) => {
      if (status !== "failed") {
        // Save to local storage
        const { token } = res
        // set token to local storage
        localStorage.setItem("jwtToken", token)
        // Set token to Auth Header
        setAuthToken(token)
        // decode token to get user data with jwt-decode
        const decoded = jwt_decode(token)
        // set current user
        return dispatch({
          type: USER_ACTIONS.GET_USER,
          payload: decoded,
        })
      }

      dispatch({
        type: USER_ACTIONS.END_REQUEST,
      })

      return errorsDispatch({
        type: ERRORS_ACTIONS.GET_ERRORS,
        payload: res.response.data,
      })
    })
  }, [submitted])

  return state
}

我试过放在console.log里面ERRORS_ACTIONS.GET_ERRORS看看反应,很好。那么我哪里出错了?

4

1 回答 1

1

useReducer允许您更好地管理复杂的状态,它不是一个状态容器,您所做的是创建 2 个不同的状态,一个在内部useLogin,另一个在您的Login组件中,errors从您的useLogin钩子返回,以便登录组件可以看到它。

登录

export default function Login({ userProps }) {
  //
  // some variables and state
  //
  const { loading, user, errors } = useLogin({ email: state.email }, state.submitted)

  useEffect(() => {
    console.log("errors", errors)
  }, [errors])

  return content
}

使用登录

export const useLogin = (data, submitted) => {
  const [state, dispatch] = useReducer(userReducer, userState)
  const [errors, errorsDispatch] = useReducer(errorsReducer, errorsState)

  useEffect(() => {
    if (!submitted) return

    dispatch({
      type: USER_ACTIONS.MAKE_REQUEST,
    })

    ticketApi.login(data).then(({ res, status }) => {
      if (status !== "failed") {
        // Save to local storage
        const { token } = res
        // set token to local storage
        localStorage.setItem("jwtToken", token)
        // Set token to Auth Header
        setAuthToken(token)
        // decode token to get user data with jwt-decode
        const decoded = jwt_decode(token)
        // set current user
        return dispatch({
          type: USER_ACTIONS.GET_USER,
          payload: decoded,
        })
      }

      dispatch({
        type: USER_ACTIONS.END_REQUEST,
      })

      return errorsDispatch({
        type: ERRORS_ACTIONS.GET_ERRORS,
        payload: res.response.data,
      })
    })
  }, [submitted])

  return { ...state, errors };
}
于 2020-10-02T03:56:17.473 回答