javascript - 返回( dispatch )未解雇

标签 javascript api redux

我是 Redux 初学者,这是我的代码:

组件:alerteProduit.js

// Map Redux state to component props
function mapStateToProps(state) {
  return {
    alertes: state.dashboardDomain.Qualite, //récupération de la liste contenu dans le state (dashboard_reducer -> Qualite)
  }
}

// Map Redux actions to component props
function mapDispatchToProps(dispatch) {
  return {
    getAlertes: () => dashboard_actions.dashboard_getAlerteProduit(), //appel méthode action vers api
  }
}

export default connect(mapStateToProps, mapDispatchToProps)(AlerteProduit);

dashboard_action.js

export function dashboard_getAlerteProduit() { 
  console.log("fired")
  return (dispatch) => {
    console.log("not fired")
    var alertes = Deserialize(storage.get("alertes"))

    if (!alertes) {//si pas d'alertes dans le store local on va les chercher dans l'api
      dashboard_api.getAlerteProduits(dispatch, storage.get("1483")) //la fonction getAlerteProduits() attend l'id du client connecté
        .then((ap) => {
          storage.set("alertes", Serialize(ap))
          dispatch(Get_AlerteProduits(ap))
        })
    } else {//si on a déjà des alertes dans le store local on les renvois
      dispatch(Get_AlerteProduits(alertes))
    }
  };
}

dashboard_api.js

export function getAlerteProduits(dispatch, idClient) {
    return auth_get_dispatch(dispatch, '/api/Produits/GetRappelProduit?idClient=' + idClient)
}

您可以在dashboard_action.js中看到,方法的开头被调用,但在“return”之后没有被调用。你知道我的项目出了什么问题吗?顺便说一下,我的 API 返回了正确的结果,因此错误不是来自这一侧。

最佳答案

我认为您的错误在mapDispatchToProps中。

getAlertes: () => dashboard_actions.dashboard_getAlerteProduit()

应该是:

getAlertes: () => dispatch(dashboard_actions.dashboard_getAlerteProduit())

更新:

此外,您需要在dashboard_getAlerteProduit 的箭头函数内返回:

if (!alertes) {//si pas d'alertes dans le store local on va les chercher dans l'api
  return dashboard_api.getAlerteProduits(dispatch, storage.get("1483")) //la fonction getAlerteProduits() attend l'id du client connecté
    .then((ap) => {
      storage.set("alertes", Serialize(ap))
      dispatch(Get_AlerteProduits(ap))
    })
} else {//si on a déjà des alertes dans le store local on les renvois
  return dispatch(Get_AlerteProduits(alertes))
}

关于javascript - 返回( dispatch )未解雇,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47156077/

相关文章:

javascript - 伪元素操作

javascript - 同时解构和传入完整对象

python - 获取特定 NCT ID 病史的临床试验

javascript - 如何使用 Redux Saga debounce 但首先获取而不是等待 n 毫秒

javascript - react-redux 嵌套组件不是父组件中的 ReactClass?

javascript - 从父级和子级 Http 请求获取可观察数据

javascript - AttachEvent 在 Chrome 浏览器中不适用于焦点事件

api - 通过 Rest API 以 csv 格式导出 PowerBI 报告

php - 将 Mysql 附近查询转换为 Laravel API 查询

reactjs - React Redux Material-ui - 制作带有可编辑单元格的表格