Skip to content

Instantly share code, notes, and snippets.

@tjunghans
Created September 16, 2024 12:02
Show Gist options
  • Save tjunghans/de591c8b4f6523ec2bccaf4d9506709c to your computer and use it in GitHub Desktop.
Save tjunghans/de591c8b4f6523ec2bccaf4d9506709c to your computer and use it in GitHub Desktop.
React Reducer
export enum ActionType {
CreateSomething,
UpdateSomething,
DeleteSomething,
RetrieveSomething
}
import { Dispatch } from "react";
type Something = { name: string; };
export type State = {
somethings: Something[];
};
export type CreateSomething = {
type: ActionType.CreateSomething;
payload: {
name: string;
};
};
export type UpdateSomething = {
type: ActionType.UpdateSomething;
payload: {
id: string;
name: string;
};
};
export type DeleteSomething = {
type: ActionType.DeleteSomething;
payload: {
id: string;
};
};
export type RetrieveSomething = {
type: ActionType.RetrieveSomething;
payload: {
id: string;
};
};
type ReducerActions = CreateSomething | UpdateSomething | DeleteSomething | RetrieveSomething;
export type Dispatcher = Dispatch<ReducerActions>;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment