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());
});
}
它以前工作但是当我重新启动服务器时它停止工作了。