react-hooks-global-state
Advanced tools
Comparing version 0.0.4 to 0.0.5
@@ -5,2 +5,6 @@ # Change Log | ||
## [0.0.5] - 2018-11-06 | ||
### Changed | ||
- Store enhancer support | ||
## [0.0.4] - 2018-11-05 | ||
@@ -7,0 +11,0 @@ ### Changed |
{ | ||
"name": "react-hooks-global-state", | ||
"description": "Simple global state for React by Hooks API", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"author": "Daishi Kato", | ||
@@ -25,3 +25,4 @@ "repository": { | ||
"examples:onmount": "webpack-dev-server --entry ./examples/05_onmount/main.ts --content-base examples/05_onmount", | ||
"examples:reducer": "webpack-dev-server --entry ./examples/06_reducer/main.ts --content-base examples/06_reducer" | ||
"examples:reducer": "webpack-dev-server --entry ./examples/06_reducer/main.ts --content-base examples/06_reducer", | ||
"examples:middleware": "webpack-dev-server --entry ./examples/07_middleware/main.ts --content-base examples/07_middleware" | ||
}, | ||
@@ -60,2 +61,3 @@ "keywords": [ | ||
"react-use": "^3.1.0", | ||
"redux": "^4.0.1", | ||
"ts-loader": "^5.3.0", | ||
@@ -62,0 +64,0 @@ "tslint": "^5.11.0", |
@@ -62,4 +62,4 @@ react-hooks-global-state | ||
switch (action.type) { | ||
case: 'increment': return { ...state, counter: state.counter + 1 }; | ||
case: 'decrement': return { ...state, counter: state.counter - 1 }; | ||
case 'increment': return { ...state, counter: state.counter + 1 }; | ||
case 'decrement': return { ...state, counter: state.counter - 1 }; | ||
default: return state; | ||
@@ -66,0 +66,0 @@ } |
@@ -7,9 +7,14 @@ export type StateItemUpdater<T> = (f: ((v: T) => T) | T) => void; | ||
export type Dispatch<A> = (action: A) => A; | ||
export type Store<S, A> = { | ||
stateItemHooks: { [K in keyof S]: StateItemHook<S[K]> }, | ||
getState: () => S, | ||
dispatch: (action: A) => void, | ||
dispatch: Dispatch<A>, | ||
}; | ||
export type Enhancer<S, A> = (store: Store<S, A>) => Store<S, A>; | ||
export type StoreCreator<S, A> = (reducer: Reducer<S, A>, initialState: S) => Store<S, A>; | ||
export type Enhancer<S, A> = (creator: StoreCreator<S, A>) => StoreCreator<S, A>; | ||
export type CreateGlobalState = <S extends {}, A extends {}>(initialState: S) => { | ||
@@ -21,9 +26,8 @@ stateItemHooks: { [K in keyof S]: StateItemHook<S[K]> }, | ||
export type CreateStore = <S extends {}, A extends {}>( | ||
reducer: Reducer<S, A>, initialState: S, enhancer?: Enhancer<S, A>) => { | ||
stateItemHooks: { [K in keyof S]: StateItemHook<S[K]> }, | ||
getState: () => S, | ||
dispatch: (action: A) => void; | ||
}; | ||
reducer: Reducer<S, A>, | ||
initialState: S, | ||
enhancer?: Enhancer<S, A>, | ||
) => Store<S, A>; | ||
export const createGlobalState: CreateGlobalState; | ||
export const createStore: CreateStore; |
@@ -11,3 +11,2 @@ import { useState, useEffect } from 'react'; | ||
const isFunction = fn => (typeof fn === 'function'); | ||
const defaultEnhancer = store => store; | ||
@@ -65,8 +64,10 @@ // core functions | ||
const newState = reducer(oldState, action); | ||
if (oldState === newState) return; | ||
keys.forEach((key) => { | ||
if (oldState[key] !== newState[key]) { | ||
stateItemMap[key].updater(newState[key]); | ||
} | ||
}); | ||
if (oldState !== newState) { | ||
keys.forEach((key) => { | ||
if (oldState[key] !== newState[key]) { | ||
stateItemMap[key].updater(newState[key]); | ||
} | ||
}); | ||
} | ||
return action; | ||
}; | ||
@@ -86,3 +87,4 @@ return dispatch; | ||
export const createStore = (reducer, initialState, enhancer = defaultEnhancer) => { | ||
export const createStore = (reducer, initialState, enhancer) => { | ||
if (enhancer) return enhancer(createStore)(reducer, initialState); | ||
const stateItemMap = map(initialState, createStateItem); | ||
@@ -93,4 +95,5 @@ const getState = createGetState(stateItemMap, initialState); | ||
stateItemHooks: Object.freeze(map(stateItemMap, x => x.hook)), | ||
...enhancer({ getState, dispatch }), | ||
getState, | ||
dispatch, | ||
}; | ||
}; |
@@ -5,4 +5,6 @@ { | ||
"lib": ["es2015", "dom"], | ||
"jsx": "react" | ||
"jsx": "react", | ||
"noUnusedLocals": true, | ||
"noUnusedParameters": true | ||
} | ||
} |
@@ -9,4 +9,5 @@ { | ||
"interface-over-type-literal": false, | ||
"variable-name": [true, "ban-keywords", "check-format", "allow-pascal-case"] | ||
"variable-name": [true, "ban-keywords", "check-format", "allow-pascal-case"], | ||
"prefer-array-literal": [true, { "allow-type-parameters": true }] | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
36855
55
993
29