【问题标题】:Redux dispatch never run in React Native appRedux dispatch 永远不会在 React Native 应用程序中运行
【发布时间】:2022-01-02 03:27:27
【问题描述】:

我在一个 react 本机应用程序上有一个身份验证操作,它必须在身份验证期间执行另一个操作,但它永远不会执行 (dispatch(getMobiles()))。我不理解为什么。你有什么想法吗?

如果我的身份验证顺利,我想立即检索我的新用户的数据,所以我想执行另一个操作 getMobiles()。

提前感谢:)

身份验证操作

export const authentication = (
  username: String,
  password: String,
  label: String,
  synchro: Boolean,
  url: String,
) => {
  return dispatch => {
    dispatch({type: LOGIN.PENDING, payload: ''});

    const type = UDA_URL_LIST.map(uda => {
      if (uda.url === url) {
        return uda.name;
      }
    })
      .join()
      .replace(/[, ]+/g, ' ')
      .trim();

    fetchUser(url, username.trim(), password.trim())
      .then(response => {
        if (!response.err) {
          const newUser = {
            ...response,
            label,
            type,
            synchro,
          };
          dispatch({type: LOGIN.SUCCESS, payload: newUser});

          // not dispatched !
          return dispatch(getMobiles(url, response.key, newUser.userId));
        }
      })
      .catch(err => dispatch({type: LOGIN.ERROR, payload: err}));
  };
};

getMobiles

export const getMobiles = (
  url: String | null = null,
  token: String,
  userId: String,
) => {
  return dispatch => {
    dispatch({type: MOBILES.PENDING, payload: ''});
    fetchMobiles(url, token)
      .then(mobilesList => {
        dispatch({
          type: MOBILES.SUCCESS,
          payload: mobilesList.data,
          meta: {userId},
        });
      })
      .catch(err => alert(err));
  };
};
};

【问题讨论】:

    标签: javascript reactjs react-native redux react-redux


    【解决方案1】:

    您在 getMobiles 中的代码需要使用参数调度进行第二次调用, 尝试使用getMobiles(url, response.key, newUser.userId)(dispatch)

    【讨论】:

    • 感谢您的回复,但不幸的是它对我不起作用
    猜你喜欢
    • 1970-01-01
    • 2019-02-11
    • 2016-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多