Hi,
My store no longer renders after I added the search feature in the extra challenge question. I reviewed my code and searched the boards, but I can’t figure it out. Any ideas as to where I went wrong?
–John
App.js
import React from 'react';
import { Inventory } from '../features/inventory/Inventory.js';
import { CurrencyFilter } from '../features/currencyFilter/CurrencyFilter.js';
// Import the Cart component here.
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;
const filteredInventory = getFilteredItems(state.inventory, state.searchTerm);
return (
<div>
<CurrencyFilter
currencyFilter={state.currencyFilter}
dispatch={dispatch}
/>
<Inventory
inventory={filteredInventory}
currencyFilter={state.currencyFilter}
dispatch={dispatch}
/>
<Cart
currencyFilter={state.currencyFilter}
cart={state.cart}
dispatch={dispatch}
/>
<SearchTerm
SearchTerm={state.searchTerm}
dispatch={dispatch}
/>
</div>
);
};
function getFilteredItems(items, searchTerm) {
return items.filter(items => items.name.toLowerCase().includes(searchTerm.toLowerCase()));
}
store.js
// Import createStore and combineReducers here.
import { createStore, combineReducers} from 'redux';
// Import the slice reducers here.
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';
// Create and export the store here.
const reducers = {
inventory: inventoryReducer,
cart: cartReducer,
currencyFilter: currencyFilterReducer,
searchTerm: searchTermReducer
};
const rootReducer = combineReducers(reducers);
export const store = createStore(rootReducer);
SearchTerm.js
import React from 'react';
import { setSearchTerm, clearSearchTerm } from './searchTermSlice.js';
const searchIconUrl =
'https://static-assets.codecademy.com/Courses/Learn-Redux/Recipes-App/icons/search.svg';
const clearIconUrl =
'https://static-assets.codecademy.com/Courses/Learn-Redux/Recipes-App/icons/clear.svg';
export const SearchTerm = (props) => {
const { searchTerm, dispatch } = props;
const onSearchTermChangeHandler = (e) => {
const userInput = e.target.value;
dispatch(setSearchTerm(userInput));
};
const onClearSearchTermHandler = () => {
dispatch(clearSearchTerm());
};
return (
<div id="search-container">
<img id="search-icon" alt="" src={searchIconUrl} />
<input
id="search"
type="text"
value={searchTerm}
onChange={onSearchTermChangeHandler}
placeholder="Search products"
/>
{searchTerm.length > 0 && (
<button
onClick={onClearSearchTermHandler}
type="button"
id="search-clear-button"
>
<img src={clearIconUrl} alt="" />
</button>
)}
</div>
);
};
searchTermSlice
const initialSearchTerm = '';
export const searchTermReducer = (state = initialSearchTerm, action) => {
switch (action.type) {
case 'searchTerm/setSearchTerm':
return action.payload;
case 'searchTerm/clearSearchTerm':
return '';
default: return state;
};
};
export const setSearchTerm = (term) => {
return {
type: 'searchTerm/setSearchTerm',
payload: term
}
};
export const clearSearchTerm = () => {
return {
type: 'searchTerm/clearSearchTerm'
}
};