redux的action组件中调用的方法不起作用

问题描述 投票:0回答:0
const getProducts = () => async (dispatch) => { 
    try { 
           const data = await fetch("http://localhost:8005/getproducts", 
           {  method: "GET", 
              headers: {     
                 "Content-Type": "application/json"
               }
           });
           const res = await data.json();
           console.log(res);
           dispatch({ type: "SUCCESS_GET_PRODUCTS", payload: res });
    } catch (error) {
           dispatch({ type: "FAIL_GET_PRODUCTS", payload: error.response });
     }
}
export default getProducts;

此方法无效,在 action.js 文件中 并从这里被调用。

const { products } = useSelector(state => state.getProReducer);
console.log(products)
const dispatch = useDispatch();

useEffect(() => {
    dispatch(getProducts());
});

}

它以前工作但是当我重新启动服务器时它停止工作了。

reactjs redux action dispatch redux-reducers
© www.soinside.com 2019 - 2024. All rights reserved.