I’m encountering the following error in my React Native code:
ERROR Error: Actions may not have an undefined "type" property. You may have misspelled an action type string constant.
This error seems to be related to actions in my Redux setup, but I’m not sure how to resolve it. Here’s a simplified version of my code:
//ActionTypes
export const ActionTypes = {
export const GET_ADDRESSES_LOADING = 'GET_ADDRESSES_LOADING'
export const GET_ADDRESSES_SUCCESS = 'GET_ADDRESSES_SUCCESS'
export const GET_ADDRESSES_FAIL = 'GET_ADDRESSES_FAIL'
};
//Action
import axiosInstance from '../../api/axiosInstance'
import {
GET_ADDRESSES_LOADING,
GET_ADDRESSES_SUCCESS,
GET_ADDRESSES_FAIL
} from '../actionTypes'
export default () => dispatch => {
dispatch({
type: GET_ADDRESSES_LOADING,
})
axiosInstance.get(`getAddresses`).then(res => {
console.log('GET_ADDRESSES:', res.data)
dispatch({
type: GET_ADDRESSES_SUCCESS,
payload: res.data,
})
}).catch(err => {
console.log('Error response status getAddresses:', err.response?.status)
console.log('Error response data getAddresses:', err.response?.data)
dispatch({
type: GET_ADDRESSES_FAIL,
payload: err.response
? err.response.data
: {error: 'Something went wrong, try again!'},
})
})
}
//Reducers
import {GET_ADDRESSES_LOADING,
GET_ADDRESSES_SUCCESS,
GET_ADDRESS_DETAIL_FAIL,} from '../actionTypes'
import homeState from '../initialState/homeState'
const reducer = (state = initialState, action) => {
case GET_ADDRESSES_LOADING:
return {
...state,
getAddresses: {
...state.getAddresses,
loading: true,
},
}
case GET_ADDRESSES_SUCCESS:
return {
...state,
getAddresses: {
...state.getAddresses,
data: payload.userAddress,
loading: false,
error: null,
},
}
case GET_ADDRESSES_FAIL:
return {
...state,
getAddresses: {
...state.getAddresses,
loading: false,
error: payload,
},
}
//Store
const rootReducer = combineReducers({
token: tokenReducer,
home: homeReducer,
auth: authReducer,
// Add more reducers if needed
});
const persistConfig = {
key : 'root',
storage : AsyncStorage,
whiteList : ['whitelist']
};
const persistedReducer = persistReducer(persistConfig,rootReducer);
export const store = createStore(
persistedReducer,
applyMiddleware(thunk) // Apply Redux Thunk middleware
);
export const persistor = persistStore(store);
I’ve double-checked my action types, and they seem to be correctly defined in ActionTypes. What could be causing this error, and how can I fix it?