Just started to learn redux, I don't want to use react-redux right now, i'm just using redux. I'm trying to dispatch an action to fill the cards with numbers, I don't understand why it isn't working. when I dispatch in the store.js file it works but not in the Game component.
store.js :
import { createStore } from "redux";
const state = {
cards: [],
player: "mor",
computer: { name: "computer", cards: [] },
};
const reducer = (state, action) => {
switch (action.type) {
case "SET_CARDS":
return {
...state,
cards: action.payload.initCards(),
};
default:
break;
}
};
const store = createStore(reducer, state);
export { store };
Game.js:
import React from "react";
import { useEffect } from "react";
import { store } from "./store";
const NUM_OF_CARDS = 52;
const MAX_CARD_VALUE = 13;
function Game() {
//shuffle cards with Fisher Yates algorithm
const shuffle = (array) => {
for (let i = array.length - 1; i > 0; i--) {
let j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
};
const setCards = {
type: "SET_CARDS",
payload: {
initCards: () => {
const cards = Array(NUM_OF_CARDS / MAX_CARD_VALUE)
.fill(
Array(13)
.fill()
.map((_, i) => i + 1)
)
.flat();
shuffle(cards);
return cards;
},
},
};
useEffect(() => {
store.dispatch(setCards);
}, []);
return (
<div>
{store.getState().cards?.map((card) => (
<div>{card}</div>
))}
</div>
);
}
export default Game;
index.js:
import React from "react";
import ReactDOM from "react-dom";
import "./index.css";
import App from "./App";
import { store } from "./store";
const render = () => ReactDOM.render(<App />, document.getElementById("root"));
store.subscribe(render);
render();
I get an error that I can't use the map function on cards... thanks.