Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing a value from an array using redux toolkit

I am new to using "@reduxjs/toolkit" (version "^1.5.1").

I am trying to remove an object from within the state's array (roundScore). This is usually something that is very simple to do using filter(). For some reason this isn't working and I can't figure out why. Here's my code:

Reducer slice:

import { createSlice } from "@reduxjs/toolkit";

export const roundScoreSlice = createSlice({
    name: "roundScore",
    initialState: {
        roundScore: [],
    },

    reducers: {
        deleteArrow: (state, action) => {
            console.log(`action.payload = ${action.payload}`); // returns correct id

            state.roundScore.filter((arrow) => arrow.id !== action.payload);
        },
    },
});

export const { deleteArrow } = roundScoreSlice.actions;

export default roundScoreSlice.reducer;

React component:

import React from "react";
import styled from "styled-components";
import { motion } from "framer-motion";
import { useDispatch } from "react-redux";
import { deleteArrow } from "../../redux-reducers/trackSession/roundScoreSlice";

export default function InputtedScore({
    arrowScore,
    id,
    initial,
    animate,
    variants,
}) {
    const dispatch = useDispatch();

    const applyStyling = () => {
        switch (arrowScore) {
            case 0:
                return "miss";
            case 1:
                return "white";
            case 2:
                return "white";
            case 3:
                return "black";
            case 4:
                return "black";
            case 5:
                return "blue";
            case 6:
                return "blue";
            case 7:
                return "red";
            case 8:
                return "red";
            case 9:
                return "gold";
            case 10:
                return "gold";
            default:
                return null;
        }
    };

    return (
        <ParentStyled
            id={id}
            initial={initial}
            animate={animate}
            variants={variants}
            onClick={() => dispatch(deleteArrow(id))}
        >
            <Circle className={applyStyling()}>
                {arrowScore}
                <IconStyled>
                    <IoIosClose />
                </IconStyled>
                <IoIosClose className="redCross" />
            </Circle>
        </ParentStyled>
    );
}

The state after adding 2 arrows would look like this:

roundScore: [
    {
        id:"e0f225ba-19c2-4fd4-b2bf-1e0aef6ab4e0"
        arrowScore:7
    },

    {
        id:"2218385f-b37a-4f2c-a8db-4e7e65846171"
        arrowScore:5
    }
]

I've tried a combination of things.

  • Using e.target.id within dispatch
  • Using e.currentTarget.id within dispatch
  • Using ({id}) instead of just (id) within dispatch
  • Wrapping the reducer function with or without braces e.g. within (state, action) => { /* code */ }

What is it I'm missing? I know this is going to be a simple fix but for some reason it's eluding me.

Any help is much appreciated.

like image 255
srWebDev Avatar asked May 06 '26 10:05

srWebDev


1 Answers

Okay, it looks like the issue is in the way how filter method works, it returns a new array, and an initial array is not mutated (that's why we have been using filter before to manipulate redux state), also in the code you've shown value of the filtering operation not assigned to any property of your state You need to assign the value or mutate array, so the code below should work for you

state.roundScore = state.roundScore.filter((arrow) => arrow.id !== action.payload);

Mutate your existing array:

state.roundScore.splice(state.roundScore.findIndex((arrow) => arrow.id === action.payload), 1);
like image 135
Dmytro Krasnikov Avatar answered May 09 '26 03:05

Dmytro Krasnikov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!