{"id":22269084,"url":"https://github.com/jason89521/quickly-use-reducer","last_synced_at":"2025-03-25T15:21:35.179Z","repository":{"id":65522278,"uuid":"484638337","full_name":"jason89521/quickly-use-reducer","owner":"jason89521","description":"Quickly generate a reducer and corresponding action creators","archived":false,"fork":false,"pushed_at":"2022-04-24T04:43:31.000Z","size":3,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-03-02T21:48:23.624Z","etag":null,"topics":["action-creators","reducer","use-reducer"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jason89521.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-04-23T03:45:12.000Z","updated_at":"2023-03-07T04:36:24.000Z","dependencies_parsed_at":"2023-01-27T06:00:49.624Z","dependency_job_id":null,"html_url":"https://github.com/jason89521/quickly-use-reducer","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jason89521%2Fquickly-use-reducer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jason89521%2Fquickly-use-reducer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jason89521%2Fquickly-use-reducer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jason89521%2Fquickly-use-reducer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jason89521","download_url":"https://codeload.github.com/jason89521/quickly-use-reducer/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245486365,"owners_count":20623244,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["action-creators","reducer","use-reducer"],"created_at":"2024-12-03T11:15:04.254Z","updated_at":"2025-03-25T15:21:35.145Z","avatar_url":"https://github.com/jason89521.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Quickly Use Reducer\n\n[![npm](https://img.shields.io/npm/v/quickly-use-reducer)](https://www.npmjs.com/package/quickly-use-reducer)\n\nUse this package to generate reducer and action creators automatically. All you need to do is passing the initial state and an object which contains all case reducers.\n\n## Example\n\n```tsx\nimport type { PayloadAction } from 'quickly-use-reducer';\nimport { createSlice } from 'quickly-use-reducer';\n\nconst slice = createSlice([] as Todo[], {\n  setTodos: (state, action: PayloadAction\u003cTodo[]\u003e) =\u003e action.payload,\n  addTodo: (state, action: PayloadAction\u003cTodo\u003e) =\u003e [...state, action.payload],\n  checkTodo: (state, action: PayloadAction\u003c{ id: string; checked: boolean }\u003e) =\u003e {\n    const { id, checked } = action.payload;\n    return state.map(todo =\u003e {\n      if (todo.id === id) return { ...todo, isCompleted: checked };\n\n      return todo;\n    });\n  },\n  deleteTodo: (state, action: PayloadAction\u003cstring\u003e) =\u003e state.filter(todo =\u003e todo.id !== action.payload),\n});\n\nconst {\n  initialState,\n  actionCreators: { setTodos, addTodo, checkTodo, deleteTodo },\n  reducer,\n} = slice;\n\nconst TodoApp = () =\u003e {\n  const [todos, dispatch] = useReducer(reducer, initialState);\n\n  return (\n    \u003cdiv\u003e\n      {todos.map(todo =\u003e (\n        \u003cdiv key={todo.id} onClick={() =\u003e dispatch(deleteTodo(todo.id))}\u003e\n          {todo.content}\n        \u003c/div\u003e\n      ))}\n    \u003c/div\u003e\n  );\n};\n```\n\n## API reference\n\n### `createSlice`\n\n#### Parameters\n\n`createSlice` accepts two parameters, and these parameters are all required.\n\n##### `initialState`\n\nThe initial state of your reducer. It helps `createSlice` to generate correct action creators.\n\n##### `reducers`\n\nAn object contains all your case reducers. A case reducer is a piece of the main reducer, it works like a normal reducer, but only handles one case.\n\nA case reducer can accepts 0 ~ 2 parameters, and should return a new state with the same type of the initial state.\n\n```ts\nconst slice = createSlice(0, {\n  // 0 parameter\n  reset: () =\u003e 0,\n  // 1 parameter\n  increment: state =\u003e state + 1,\n  // 2 parameters\n  // typescript: (state, action: PayloadAction\u003cnumber\u003e) =\u003e state + action.payload\n  add: (state, action) =\u003e state + action.payload,\n});\n```\n\n#### Return\n\n`createSlice` will return an object which contains `initialState`, `actionCreators` and `reducer`.\n\n##### `initialState`\n\nThe same as the first parameter.\n\n##### `actionCreators`\n\nA object contains all action creators, their name are the same as the `reducers`' property. For example the above `slice` will generate an `actionCreators` object which contains `reset`, `increment` and `add`.\n\n```ts\nconst { reset, increment, add } = slice.actionCreators;\n```\n\n##### `reducer`\n\nA reducer function. You can pass it to `useReducer` directly.\n\n```ts\nconst [state, dispatch] = useReducer(slice.reducer, slice.initialState);\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjason89521%2Fquickly-use-reducer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjason89521%2Fquickly-use-reducer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjason89521%2Fquickly-use-reducer/lists"}