Skip to content

Instantly share code, notes, and snippets.

@kyktommy
Last active July 21, 2020 01:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kyktommy/9e6fea3fd3c4d26464f477156b1500a6 to your computer and use it in GitHub Desktop.
Save kyktommy/9e6fea3fd3c4d26464f477156b1500a6 to your computer and use it in GitHub Desktop.
redux typescript
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
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