I have tried everything I can think of but I cannot for the life of me figure out why this is not rendering. Was hoping someone could shed some light on this, I feel that I am missing something obvious but I can’t see it. I will post all the files we worked on in the project so that you can see them all. Any insight here is much appreciated…
export const addItem = (itemToAdd) => {
return {
type: "cart/addItem",
payload: itemToAdd,
};
};
export const changeItemQuantity = (name, newQuantity) => {
return {
type: "cart/changeItemQuantity",
payload: {
name,
newQuantity,
},
};
};
const initialCart = {};
export const cartReducer = (cart = initialCart, action) => {
switch (action.type) {
case "cart/addItem": {
const { name, price } = action.payload;
// if the item already exists, increase the quantity by 1, otherwise set it to 1
const quantity = cart[name] ? cart[name].quantity + 1 : 1;
const newItem = { price, quantity };
// Add the new item to the cart (or replace it if it existed already)
return {
...cart,
[name]: newItem,
};
}
case "cart/changeItemQuantity": {
const { name, newQuantity } = action.payload;
const itemToUpdate = cart[name];
const updatedItem = {
...itemToUpdate,
quantity: newQuantity,
};
return {
...cart,
[name]: updatedItem,
};
}
default: {
return cart;
}
}
};
import { createStore, combineReducers } from "redux";
import { inventoryReducer } from "../features/inventory/inventorySlice.js";
import { cartReducer } from "../features/cart/cartSlice.js";
import { currencyFilterReducer } from "../features/currencyFilter/currencyFilterSlice.js";
import { searchTermReducer } from "../features/searchTerm/searchTermSlice.js";
export const store = createStore(
combineReducers({
inventory: inventoryReducer,
cart: cartReducer,
currencyFilter: currencyFilterReducer,
searchTerm: searchTermReducer,
})
);
import React from "react";
import ReactDOM from "react-dom";
import { App } from "./app/App.js";
import { store } from "./app/store.js";
const render = () => {
ReactDOM.render(
<App state={store.getState()} dispatch={store.dispatch} />,
document.getElementById("root")
);
};
render();
store.subscribe(render);
import React from "react";
import { Inventory } from "../features/inventory/Inventory.js";
import { CurrencyFilter } from "../features/currencyFilter/CurrencyFilter.js";
import { Cart } from "../features/cart/Cart.js";
import { SearchTerm } from "../features/searchTerm/SearchTerm.js";
// Render the Cart component below <Inventory />
export const App = (props) => {
const { state, dispatch } = props;
return (
<div>
<CurrencyFilter
currencyFilter={state.currencyFilter}
dispatch={dispatch}
/>
<SearchTerm searchTerm={state.searchTerm} dispatch={dispatch} />
<Inventory
inventory={state.inventory}
currencyFilter={state.currencyFilter}
dispatch={dispatch}
/>
<Cart
cart={state.cart}
currencyFilter={state.currencyFilter}
dispatch={dispatch}
/>
</div>
);
};
export const searchTermReducer = (state = initialState, action) => {
switch (action.type) {
case "searchTerm/setSearchTerm": {
const newTerm = action.payload;
return newTerm;
}
case "searchTerm/clearSearchTerm":
{
return "";
}
return state;
}
};
export const setSearchTerm = (term) => {
type: "searchTerm/setSearchTerm";
payload: term;
};
export const clearSearchTerm = () => {
type: "searchTerm/clearSearchTerm";
};
Thanks in advance…