简体   繁体   English

退货(发货)未触发

[英]Return (dispatch) not fired

I am beginner with Redux and this is my code : 我是Redux的初学者,这是我的代码:

Component : alerteProduit.js 组件: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 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 dashboard_api.js

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

You can see in the dashboard_action.js, the beginning of the method is called but not after the "return". 您可以在dashboard_action.js中看到,该方法的开始被调用,但在“返回”之后未被调用。 Do you know what is wrong in my project ? 您知道我的项目有什么问题吗? By the way my API is returning the correct result so the error is not from this side. 顺便说一句,我的API返回的是正确的结果,因此错误不是来自这一方面。

I think your error is in mapDispatchToProps . 我认为您的错误是在mapDispatchToProps中

getAlertes: () => dashboard_actions.dashboard_getAlerteProduit()

It should be: 它应该是:

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

Update: 更新:

Also, you need a return inside the arrow function at 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))
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM