Answer the question
In order to leave comments, you need to log in
Should you use TypeScript in Redux?
Hello.
There is a small application. I do (that is, I can say I have already done) for myself.
I recently introduced a typescript into it, and I would like to know right away whether it makes sense to use typescript in reducers (wouldn’t it work out that I’m doing this for some stupid exercise that is not quoted anywhere?) and if so, how to type it. Any guides and best practices?
Thanks for answers.
Answer the question
In order to leave comments, you need to log in
I use Flow in a project.
Phot what an example of App reducer looks like.
//@flow
import type {
ActionWithPayloadType,
AppStateType
} from '@project/types';
import { OPEN_MODAL, CLOSE_MODAL, SET_USER_LOGIN } from '../constants';
const initialState: AppStateType = {
isModalOpen: false,
isUserLoggedIn: false
};
//eslint-disable-next-line complexity
const appReducer = (
state: AppStateType = initialState,
action: ActionWithPayloadType = { type: '', payload: '' }
) : AppStateType => {
if (!action) { return state; }
if (action.type === OPEN_MODAL) {
return { ...state, isModalOpen: true };
}
if (action.type === CLOSE_MODAL) {
return { ...state, isModalOpen: false };
}
if (action.type === SET_USER_LOGIN) {
return { ...state, isUserLoggedIn: action.payload };
}
return state;
};
export default appReducer;
export type ActionWithPayloadType = {
type: string,
payload: *
};
export type AppStateType = {
isModalOpen: boolean,
isUserLoggedIn: boolean
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question