0

While dispatching the event I am facing the issue "Argument of type '(dispatch: any) => Promise' is not assignable to parameter of type 'AnyAction'." I am new to React 18 and redux toolkit. Using Redux-thunk, everything seems fine, but dispatching the event gives an error: The error is shown in the Action file while dispatching . Seems the issue is with return type. Not able to find a solution for it.

import { Provider } from 'react-redux';
import { composeWithDevTools } from 'redux-devtools-extension';
import { legacy_createStore as createStore , applyMiddleware  } from 'redux';
import thunk from 'redux-thunk';
const store = createStore(rootReducer,composeWithDevTools(applyMiddleware(thunk)));
    ReactDOM.render(<Provider store={store}>
      <App/>
    </Provider>,document.getElementById('root'));

PlayerDetails.Action.tsx

import * as ACTION_TYPES from './action-types';
import * as CONSTANTS from '../utils/Constants'
const axios = require('axios');
const rax = require('retry-axios');

export const playerDetails_request = (data: any)=>{
    return {
        type: ACTION_TYPES.GET_PLAYER_DETAILS_REQUEST,
        payload : data
    }
}

export const playerDetails_success = (obj: any)=>{
    return {
        type: ACTION_TYPES.GET_PLAYER_DETAILS_SUCCESS,
        payload : obj
    }
}

export const playerDetails_failure = (err: any)=>{
    return {
        type: ACTION_TYPES.GET_PLAYER_DETAILS_FAILURE,
        payload : err
    }
}
export default function getPlayersDetails(data:any){
    return async function (dispatch:any) {
        dispatch(playerDetails_request(data));
        try{
            axios.defaults.withCredentials = true;
            const interceptorId = rax.attach();
            const response = await axios ({
                url:CONSTANTS.PLAYER_DETAILS,
                method:'GET',
                params: {page: '0', per_page: '25'},
                headers: CONSTANTS.header,
                //data:()

            });
         
            if(response !==null && response.status === 200){
               
                dispatch(playerDetails_success(response));
            }else{
                dispatch(playerDetails_failure(response));
            }
        }catch(error){
            console.log("ERROR");
            dispatch(playerDetails_failure("ERROR"))
        }
    }   
}

action-types.tsx

export const GET_PLAYER_DETAILS_REQUEST = "GET_PLAYER_DETAILS_REQUEST"
export const GET_PLAYER_DETAILS_SUCCESS = "GET_PLAYER_DETAILS_SUCCESS"
export const GET_PLAYER_DETAILS_FAILURE = "GET_PLAYER_DETAILS_FAILURE"
  • If you are getting confused by this code, I don't blame you. Redux thinks are confusing and unnecessary in my opinion. Consider modern alternatives, e.g. valtio : https://www.youtube.com/watch?v=gGY9HvUe2AA – basarat Dec 30 '22 at 16:39
  • in 2023 (and beyond ;) ) you should use `redux-toolkit` along with redux, all your problems are allready solved there, so unless you want to learn how the inside mechanics of redux works under the hood just use RTK query from `redux-toolkit` to handle CRUD operations for you in redux and youll get free caching out of the box as well... https://redux-toolkit.js.org/rtk-query/overview – Greg Motyl Jan 02 '23 at 06:50

0 Answers0