I have built a small app with React (redux and router). It's a marketplace (cars based). It has two main components. One showing all the cars and the other one showing the favorites. There are buttons on every item to add or remove from favorites and it works. Its removing and adding items from state on store. I can check it with redux dev tool on browser and with "console.log" from "handler" with "store.getState()".
But I'm strugling using "useSelector(state => state.move_cars)" (move_cars is my only reducer) instead of "store.getState()". When filtering favs with "store.getState()" it works and the app shows favorites. But when using "useSelector" it's undefined. So I'm gessing that i'm not using it properly but I don't know how it's not an array.
My favs component:
import React from 'react'
import style from "../components/styles/favorites.module.css";
import {store} from "../index.js"
import {move_cars} from "../"
import {useDispatch, useSelector} from "react-redux";
import {remove_car} from "../a_actions";
import cars from "../data/info/data.js";
const Favorites = () => {
const images = require.context("../data/img", true);
const dispatch = useDispatch();
const selected = useSelector(state => state.move_cars);
// const selected = store.getState();
const handler = (e) => {
// console.log("testing console from favs", selected);
console.log("testing console from favs", selected);
dispatch(remove_car(e));
}
return (
<>
<div className={style.container}>
{cars.map( x => {
// const checker = selected.some(item => item === x.Id);
if(true){
return (
<div className={style.car}>
<p className={style.textcartitle}> {x.Brand} </p>
<p className={style.textcar}>{Object.keys(x)[0]}: {x.Model} </p>
<p className={style.textcar}>{Object.keys(x)[1]}: {x.Hp} cv </p>
<p className={style.textcar}>{Object.keys(x)[3]}: {x.Year} </p>
<p className={style.textcar}>{Object.keys(x)[4]}: {x.City} </p>
<p className={style.textcar}>{Object.keys(x)[5]}: {x.Km} </p>
<img src={images(`${x.src}`)} className={style.imgin} alt="test"/>
<div value={x.Id} className={style.buttonfav} onClick={() => handler(x.Id)}>Remove from favorites</div>
</div>
);
}
})}
</div>
</>
)
}
export default Favorites
My reducer:
let initialState = [];
export const move_cars = (state = initialState, action) => {
switch(action.type){
case "add_car":
const varcheck = state.some((x) => x === action.payload);
if (!varcheck) {
return [...state, action.payload];
}
case "remove_car":
return state.filter((car_id) => car_id !== action.payload )
default:
return state;
}
}
My main index.js:
import React from 'react';
import ReactDOM from "react-dom/client";
import './index.css';
import App from './App';
import {Provider} from "react-redux";
import {move_cars} from "./a_reducers";
import {createStore} from "redux";
export const store = createStore(
move_cars,
window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()
);
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
<React.StrictMode>
<Provider store={store}>
<App />
</Provider>
</React.StrictMode>
);