I am working on an app to which will using redux saga/persist in react native to manage state and actions. I has set return state correctly, make sure the reducer return TGlobalState, avoid state is undefined with initialState but some how it keep alert error
Type ‘(state: TGlobalState | undefined, action: TActionProp<TGlobalState>) => TGlobalState’ is not assignable to type ‘TGlobalState’.
This is my Global type and reducer to handle update my global state
reducerType.ts
import {AlphaColorTheme, ColorTheme} from '../../themes/types';
export type TActionProp<T> = {
type: string;
payload?: T;
};
export type TGlobalState =
{
load: boolean;
colorScheme: ColorTheme;
colorSchemeAlpha: AlphaColorTheme;
success: boolean,
error: boolean,
successMess: string,
errorMess: string
};
globalReducer.ts
import {lightTheme, lightThemeAlpha} from '../../../themes/colorTheme';
import {ACTION} from '../../actions/actionTypes';
import type {TActionProp, TGlobalState} from '../reducerType';
const initialState: TGlobalState = {
load: false,
colorScheme: lightTheme,
colorSchemeAlpha: lightThemeAlpha,
error: false,
success: false,
successMess: '',
errorMess: '',
};
const themeReducer = (
state = initialState,
action: TActionProp<TGlobalState>,
): TGlobalState => {
switch (action.type) {
case ACTION.GLOBAL.PENDING.UPDATE_CURRENT_PENDING_STATE:
return {
...state,
load: action?.payload?.load ?? false,
};
case ACTION.GLOBAL.THEME.CHANGE_THEME_SUCCESS:
return {
...state,
load: false,
colorScheme: action?.payload?.colorScheme ?? lightTheme,
colorSchemeAlpha: action?.payload?.colorSchemeAlpha ?? lightThemeAlpha,
success: true,
successMess: action?.payload?.successMess ?? 'Update Theme successfully',
};
case ACTION.GLOBAL.THEME.CHANGE_THEME_FAIL:
return {
...state,
load: false,
error: action?.payload?.error ?? true,
errorMess: action?.payload?.errorMess ?? "There's an empty error message in store",
};
case ACTION.GLOBAL.SUCCESS.UPDATE_SUCCESS_STATE:
return {
...state,
success: action?.payload?.success ?? true,
successMess: action?.payload?.successMess ?? "There's an empty success message in store",
};
case ACTION.GLOBAL.FAILURE.UDPATE_FAILURE_STATE:
return {
...state,
error: action?.payload?.error ?? false, // Default to false for error
errorMess: action?.payload?.errorMess ?? '', // Default to empty string for error message
};
default:
return state; // Return unmodified state
}
};
export default themeReducer;
I have also tried to specify the case for state if it undefined it will return initialState and if not then return state but it not working as i expected
rootReducer.ts
import {combineReducers} from 'redux';
import themeReducer from './GlobalReducer';
import {TGlobalState} from './reducerType';
type IRootReducer = {
global: TGlobalState;
};
const rootReducer = combineReducers<IRootReducer>({
global: themeReducer,
});
export default rootReducer;
This is what it alert on my rootReducer
This is the project repository for case if anyone need a more clear vision
I really appreciate for all of your guys help in the future
3