unable to pas data (which is array) in initialisation of useReducer hook which contain array of object , Getting result in child component empty array in products initialisation.
I want same data in child component as same I passed.
code :
import React, { createContext, useContext, useEffect, useReducer, useState } from 'react'
import faker from "faker";
import { cartReducer } from './Reducer';
export const Cart = createContext();
const Context = ({ children }) => {
const [data, setProduct] = useState();
useEffect(() => {
var axios = require('axios');
var config = {
method: 'get',
url: '<apicalling here>',
headers: {
'Authorization': 'Token xyz'
}
};
axios(config)
.then(function (response) {
setProduct(response.data.cart[0].items);
})
.catch(function (error) {
console.log(error);
});
}, []);
console.warn("hello", data); //yes working, data rendering here
const [state, dispatch] = useReducer(cartReducer, {
products: data,
cart: [],
});
return <Cart.Provider value={{ state, dispatch }}>{children} </Cart.Provider>;
};
export default Context;