我是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返回正确的结果,因此错误不是来自这一方。
答案 0 :(得分:1)
我认为您的错误发生在 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))
}