Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
bcd337 committed Sep 17, 2022
1 parent 81a6c51 commit 8fc9a25
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 30 deletions.
3 changes: 0 additions & 3 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@
},
"parser": "@typescript-eslint/parser",
"parserOptions": {
"project": [
"./tsconfig.json"
],
"ecmaFeatures": {
"impliedStrict": true
},
Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-reduce-manager",
"version": "0.1.10",
"version": "0.1.11",
"license": "MIT",
"description": "Hook for React useReducer",
"repository": "github:bcd337/react-reduce-manager",
Expand Down Expand Up @@ -50,5 +50,8 @@
"ts-jest": "^29.0.1",
"ts-node": "^10.9.1",
"typescript": "^4.8"
},
"dependencies": {
"my-library-app": "file:../my-library-app"
}
}
2 changes: 1 addition & 1 deletion src/createReducer/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { default } from './createReducer'
export { default as createReducer } from './createReducer'
24 changes: 1 addition & 23 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1 @@
import { useReducer as originalUseReducer } from 'react'
import createReducer from 'createReducer'
import { createActionType, createActionCustom } from 'createAction'
import type { ActionCustomProp, Dispatch } from 'createAction/types'
import type { UseReducer } from 'types'

function useReducer<T extends object, C extends ActionCustomProp<T>>(
initialValue: T,
custom?: C,
): UseReducer<T, C> {
const reducer = createReducer(initialValue, custom ? Object.keys(custom) : [])
const [state, dispatch] = originalUseReducer(reducer, initialValue)

return {
state,
action: {
...createActionType(initialValue, dispatch as unknown as Dispatch),
...createActionCustom(dispatch as unknown as Dispatch, state, custom),
},
} as UseReducer<T, C>
}

export default useReducer
export { useReducer as default } from 'useReducer'
2 changes: 1 addition & 1 deletion src/index.test.ts → src/useReducer/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import useReducer from './index'
import useReducer from './useReducer'

describe('useReducer', () => {
test('is useReducer exist', () => {
Expand Down
1 change: 1 addition & 0 deletions src/useReducer/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default as useReducer } from './useReducer'
File renamed without changes.
23 changes: 23 additions & 0 deletions src/useReducer/useReducer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { useReducer as originalUseReducer } from 'react'
import { createReducer } from 'createReducer'
import { createActionType, createActionCustom } from 'createAction'
import type { ActionCustomProp, Dispatch } from 'createAction/types'
import type { UseReducer } from './types'

function useReducer<T extends object, C extends ActionCustomProp<T>>(
initialValue: T,
custom?: C,
): UseReducer<T, C> {
const reducer = createReducer(initialValue, custom ? Object.keys(custom) : [])
const [state, dispatch] = originalUseReducer(reducer, initialValue)

return {
state,
action: {
...createActionType(initialValue, dispatch as unknown as Dispatch),
...createActionCustom(dispatch as unknown as Dispatch, state, custom),
},
} as UseReducer<T, C>
}

export default useReducer
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
},
"files": ["./src/index.ts"],
"include": [
"src/**/*"
"src",
],
"exclude": [
"node_modules",
Expand Down

0 comments on commit 8fc9a25

Please sign in to comment.