0

I'm having a hard time to debug this cause I'm not sure if my approach is correct.. once I do some request the my action call returns me undefined action and simply not pushing my object on my redux store.

// here's my store

const initialState = {
    store: {}
}

const reducer = (state = initialState, action) => {
    switch (action.type) {
        case 'GET_DB':
            return {
                ...state,
                store: action.payload
            }
        case 'ADD_SUPPLIER':
            return {
                ...state,
                store: {
                    ...state.store,
                    supplier: [
                        ...state.store.supplier,
                        {
                            supplier_name: action.payload.supplier_name,
                            address: action.payload.address,
                            contact_person: action.payload.contact_person,
                            contact_number: action.payload.contact_number,
                            note: action.payload.note
                        }
                    ]
                }
            }
        default:
            return state;
    }
}

export default reducer;

// here's my dispatch

   const [supplier, setSupplier] = useState({
        _id: Localstorage?.result?._id, 
        token: Localstorage?.token, 
        supplier_name: '', 
        address: '', 
        contact_person: '', 
        contact_number: '', 
        note: ''
    });

dispatch(add_supplier(supplier));

export const add_supplier = (supplier) => async (dispatch) => {
    try {
        const { data } = await api.add_supplier(supplier);
        dispatch({ type: 'ADD_SUPPLIER', payload: data });
    } catch (error) {
        console.log({ 'error': error });
    }
}

I'm quite sure there's something wrong with my approach can someone please correct it cause im a newbie on redux. thanks

Gino
  • 77
  • 7
  • 1
    Please be aware that you are writing an extremely outdated (pre-2019) style of Redux. Modern Redux does not have switch..case reducers, ACTION_TYPES or immutable reducer logic - and you will probably not have problems like this there. Please follow the [official Redux tutorial](https://redux.js.org/tutorials/essentials/part-1-overview-concepts) instead. – phry Oct 05 '22 at 14:21
  • Everything looks correct to me. What exactly is going wrong? Is `ADD_SUPPLIER` actually dispatched? Does it have the expected payload? – timotgl Oct 05 '22 at 14:28
  • tbh I dont even know where I went wrong but once I dispatch if I check it on my redux logger middleware it shows that the dispatch is undefined..and idk why.. – Gino Oct 05 '22 at 14:32

0 Answers0