0

I want to remove an object from an array onClick when I click on the delete button, I write some code in my redux and react as well and it is not working !!

My reducers

import { ActionsTypes } from "../Constant/ActionsTypes";

argument(state, action)


const initialState = {
    events : [],
    days : (""),
};


export const SetEventReducer = (state = initialState, action) => {
    switch(action.type) {
      case ActionsTypes.SET_EVENTS : 
            return {... state, events : action.payload};
      default : 
        return state;
    }

};


export const trackDaysReducer = (state= initialState, action) => {
    switch(action.type) {
        case ActionsTypes.TRACK_DAYS: 
            return {... state, days : action.payload}
        default : 
            return state;
    }
};



export const removeEventReducer = (state = initialState,action) => {
    switch(action.type) {
      case ActionsTypes.REMOVE_EVENT : 
            return {}
      default : 
        return state;
    }

};

Event array represents my state array

My Event Component who contains the button

import React from 'react'
import { useSelector, useDispatch } from 'react-redux';
import { RemoveEvent } from '../Redux/Actions/ActionsEvents';

const Event = () => {
    const events = useSelector((state)=> state.allEvents.events);
    const removeEvents = useSelector((state)=> state.removeEvents);
    const dispatch = useDispatch();

    const removeEventHandler = () => {
        dispatch(RemoveEvent({}))
    }
    return (
    <section>
    {events.map((singleEvent)=> {
        const {title, id, day} = singleEvent;
        return (
            <article className="event-pop-up" key={id} >
                <h1>The Name of your Event is <span>{title}</span></h1>
                <button className="btn event"
                        onClick={removeEventHandler}>
                            Delete Event</button>
            </article>
         )
    })}
    </section>
    )
}

export default Event;

RemoveEventAction

export const RemoveEvent = (id) => {
    return {
        type : ActionsTypes.REMOVE_EVENT,
    };
};

This is the link to check out the app : https://boring-boyd-ecbeee.netlify.app/

What do you think? Thanks

2 Answers 2

1

Try this code :

in yours reducers :

export const removeEventReducer = (state = initialState,action) => {
    switch(action.type) {
      case ActionsTypes.REMOVE_EVENT : 
            return {... state, events : state.events.filter((event) => event.id !== action.payload)} // payload = id in this case
      default : 
        return state;
    }

then in your Event Component who contains the button :

import React from 'react'
import { useSelector, useDispatch } from 'react-redux';
import { RemoveEvent} from '../Redux/Actions/ActionsEvents';

const Event = () => {
    const events = useSelector((state)=> state.allEvents.events);
    const removeEvents = useSelector((state)=> state.removeEvents);
    const dispatch = useDispatch();

    const removeEventHandler = (id) => {
        dispatch(RemoveEvent(id))
    }
    return (
    <section>
    {events.map((singleEvent)=> {
        const {title, id, day} = singleEvent;
        return (
            <article className="event-pop-up" key={id} >
                <h1>The Name of your Event is <span>{title}</span></h1>
                <button className="btn event"
                        onClick={()=> removeEventHandler(id)}>
                            Delete Event</button>
            </article>
         )
    })}
    </section>
    )
}

export default Event;

then in your RemoveEvent action

export const RemoveEvent = (id) => {
    return {
        type : ActionsTypes.REMOVE_EVENT, payload: id
    };
};
Sign up to request clarification or add additional context in comments.

12 Comments

Should the action of removeEventReduce stay the same or should I pass a payload to it?
Yes pass the event id which you want to delete. and then in reducer use filter method to filter out that event from the event array. The above answer is what you need.
Yes the action's payload of removeEventReduce should equal the event's id that you want to delete
I updated my code above and i updated my action remove event and I pass id to it please check out if it is correct or not
I don't see the update, where can i check
|
0

You can remove an event using it's id.

const removeEventHandler = (id) => {
    dispatch(RemoveEvent(id))
}
return (
<section>
{events.map((singleEvent)=> {
    const {title, id, day} = singleEvent;
    return (
        <article className="event-pop-up" key={id} >
            <h1>The Name of your Event is <span>{title}</span></h1>
            <button className="btn event"
                    onClick={() => removeEventHandler(id)}>
                        Delete Event</button>
        </article>
     )
})}
</section>

After passing this id to the reducer you can loop through the events array and remove this event from array and set the new state.

And in your reducers, you can use filter() method to remove a particular event having that id

 export const removeEventReducer = (state = initialState, action) => {
    switch(action.type) {
      case ActionsTypes.REMOVE_EVENT : 
            return {... state, events : state.events.filter((event) => event.id !== action.payload)}
      default : 
        return state;
    }
}

For Remove Event Action:

export const RemoveEvent = (id) => {
    return {
        type : ActionsTypes.REMOVE_EVENT,
        payload: id,
    };
};

8 Comments

Thank you for your help but Where should I pass the id in the reducer?
@TheRealJohnDoe I have added the code for reducer in the answer. id will be passed in action param of the reducer function, which we will use in filter() method to remove that event.
I updated my code above and I updated my action remove event and I pass id to it please check out if it is correct or not
@TheRealJohnDoe I have updated the code in my answer. you need to add payload property
@TheRealJohnDoe You need to remove {} around the id
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.