Home > front end >  Typescript error while calling a redux action
Typescript error while calling a redux action

Time:01-22

I have a redux action to simply display an alert

export const showAlertConfirm = (msg) => (dispatch) => {
    dispatch({
        type: SHOW_ALERT_CONFIRM,
        payload: {
            title: msg.title,
            body: msg.body,
            response: msg.response,
            dataBag: msg.dataBag
        }
    });
};

I call this from a component

dispatch(showAlertConfirm({
            title: "my title",
            body: "my desc",
            dataBag: {
                prop: val
            }
        }));

this works as expected, however lint is avoiding to build due to this error

Type error: Argument of type '(dispatch: any) => void' is not assignable to parameter of type 'AnyAction'.

can you please tell me how to fix this error?

CodePudding user response:

Do what the message says and remove dispatch from showAlertConfirm.

export const showAlertConfirm = (msg) => {
    return {
        type: SHOW_ALERT_CONFIRM,
        payload: {
            title: msg.title,
            body: msg.body,
            response: msg.response,
            dataBag: msg.dataBag
        }
    });
};

I recommend having a look at the docs https://redux.js.org/tutorials/fundamentals/part-2-concepts-data-flow#actions

An action is a plain JavaScript object that has a type field. You can think of an action as an event that describes something that happened in the application.

  • Related