You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am new to mern stack and building a ecommerce project and got error. I could not find perfect solution, please help any one.
Below is my code.........
I am new to mern stack and building a ecommerce project and got error. I could not find perfect solution, please help any one.
Below is my code.........
// Home.jsx -- file
const product = {
name: "Blue Tshirt",
images: [{ url: "https://i.ibb.co/DRST11n/1.webp" }],
price: "$300",
_id: "uihf48"
}
const Home = () => {
const { products } = useSelector(state => state.allProduct)
useGetProduct()
return (
<>
Welcome to DigiStore
Find amazing Product Below
Scroll
)
}
export default Home
// productSlice.js--- file
import { createSlice } from "@reduxjs/toolkit";
const productSlice = createSlice({
name: "allProduct",
initialState: {
products: [],
},
reducers: {
getProduct : (state,action)=>{
state.products = action.payload
}
}
})
export const {getProduct} = productSlice.actions
export default productSlice.reducer
// custom hook file---
import axios from "axios";
import { useEffect } from "react";
import { useDispatch } from "react-redux";
import { getProduct } from "@/redux/productSlice";
const useGetProduct = ()=>{
const dispatch = useDispatch();
useEffect(()=>{
const fetchProduct = async()=>{
try {
const res = await axios.get('http://localhost:4000/api/v1/products', {withCredentials: true})
dispatch(getProduct(res.data.products))
} catch (error) {
console.log(error)
}
}
fetchProduct();
},[dispatch])
}
export default useGetProduct
also want to tell you that, products is available in my redux dev tools but cannot render on ui. Please help
Thanks in advance
The text was updated successfully, but these errors were encountered: