0

I get the array of objects coming from backend, I get it with socket.io-client. Here we go! //App.js

   import Tickers from "./Components/TickersBoard";
import { actions as tickerActions } from "./slices/tickersSlice.js";
const socket = io.connect("http://localhost:4000");

function App() {
  const dispatch = useDispatch();
  useEffect(() => {
    socket.on("connect", () => {
      socket.emit("start");
      socket.on("ticker", (quotes) => {
        dispatch(tickerActions.setTickers(quotes));
      });
    });
  }, [dispatch]);

After dispatching this array goes to Action called setTickers in the slice. //slice.js

const tickersAdapter = createEntityAdapter();
const initialState = tickersAdapter.getInitialState();
const tickersSlice = createSlice({
  name: "tickers",
  initialState,
  reducers: {
    setTickers(state, { payload }) {
      payload.forEach((ticker) => {
        const tickerName = ticker.ticker;
        const {
          price,
          exchange,
          change,
          change_percent,
          dividend,
          yeild,
          last_trade_time,
        } = ticker;

        state.ids.push(tickerName);
        const setStatus = () => {
          if (ticker.yeild > state.entities[tickerName].yeild) {
            return "rising";
          } else if (ticker.yeild < state.entities[tickerName].yeild) {
            return "falling";
          } else return "noChange";
        };
        state.entities[tickerName] = {
          status: setStatus(),
          price,
          exchange,
          change,
          change_percent,
          dividend,
          yeild,
          last_trade_time,
        };
        return state;
      });
      return state;
    },
  },
});

But the state doesn't change. I tried to log state at the beginning, it's empty. After that I tried to log payload - it's ok, information is coming to action. I tried even to do so:

setTickers(state, { payload }) {
      state = "debag";
      console.log(state);

and I get such a stack of logs in console:

debug debug debug 3 debug 2 debug

and so on.

tresor13
  • 57
  • 8

0 Answers0