Last active
July 21, 2020 01:50
-
-
Save kyktommy/9e6fea3fd3c4d26464f477156b1500a6 to your computer and use it in GitHub Desktop.
redux typescript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { createSlice, PayloadAction } from '@reduxjs/toolkit' | |
export type State = { | |
loading: boolean; | |
data: string; | |
} | |
export const initialState: State = { | |
loading: true, | |
data: '' | |
} | |
interface GetAllData { | |
data: string | |
} | |
export const slice = createSlice({ | |
name: 'counter', | |
initialState, | |
reducers: { | |
getAll: (state, action: PayloadAction<GetAllData>) { | |
state.data = action.payload.data | |
} | |
} | |
}) | |
export const { getAll } = slice.actions | |
// getAll('something') | |
export default slice.reducer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { AnyAction } from 'redux'; | |
import { createReducer, createActions } from 'reduxsauce'; | |
interface ActionTypes { | |
GET_ALL: 'GET_ALL'; | |
} | |
interface GetAllAction extends AnyAction { | |
type: ActionTypes['GET_ALL']; | |
payload: { | |
data: string; | |
}; | |
} | |
interface Actions { | |
getAll(data: string): GetAllAction; | |
} | |
const { Types, Creators } = createActions<ActionTypes, Actions>({ | |
getAll: ['data'], | |
}); | |
// Creators.getAll('something') | |
export type State = { | |
loading: boolean; | |
data: string; | |
} | |
export const initialState: State = { | |
loading: true, | |
data: '' | |
} | |
export const getAll = (state: State, action: GetAllAction):State => { | |
return { ...state, loading: true, data: action.payload.data } | |
} | |
export const reducer = createReducer(initialState, { | |
[Types.GET_ALL]: getAll | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment