我是 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/