-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Independence reshow-flux-base support
- Loading branch information
Showing
20 changed files
with
406 additions
and
662 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,74 +1,71 @@ | ||
# `createReducer` | ||
|
||
> ` | ||
> Similar with react useReducer, but let you use anywhere. | ||
> ` | ||
* GIT | ||
* https://github.com/react-atomic/reshow/tree/main/packages/reshow-flux-base | ||
* NPM | ||
* https://www.npmjs.com/package/reshow-flux-base | ||
|
||
> `Similar with react useReducer, but let you use anywhere.` | ||
- GIT | ||
- https://github.com/react-atomic/reshow/tree/main/packages/reshow-flux-base | ||
- NPM | ||
- https://www.npmjs.com/package/reshow-flux-base | ||
|
||
## Usage | ||
|
||
Accepts a reducer of type `(state, action) => newState`, and returns the current store with a dispatch method. | ||
Accepts a reducer function of type `(state, action) => newState`. | ||
Returns a new store with a dispatch method. | ||
|
||
```js | ||
import { createReducer } from "reshow-flux-base"; | ||
const [store, dispatch] = createReducer(reducer, initial[Arg|Function]); | ||
|
||
/** | ||
* reducer -> (state, action) => newState | ||
*/ | ||
|
||
const [store, dispatch] = createReducer(reducer, initial[Arg | Function]); | ||
``` | ||
|
||
### `Store` methods. | ||
| *Methods* | *Explain* | | ||
| --- | --- | | ||
| getState | Return current state. | | ||
| addListener |You could register any callback function such as react useState. | | ||
| removeListener | Remove register callback, such as unmount a component. | | ||
|
||
| _Methods_ | _Explain_ | | ||
| -------------- | ---------------------------------------------------------------- | | ||
| getState | Return current state. | | ||
| addListener | You could register any callback function such as react useState. | | ||
| removeListener | Remove register callback, such as unmount a component. | | ||
|
||
## `Full App Example` | ||
|
||
```js | ||
const initialState = {count: 0}; | ||
const initialState = { count: 0 }; | ||
|
||
function reducer(state, action) { | ||
switch (action.type) { | ||
case 'increment': | ||
return {count: state.count + 1}; | ||
case 'decrement': | ||
return {count: state.count - 1}; | ||
default: | ||
throw new Error(); | ||
} | ||
switch (action.type) { | ||
case "increment": | ||
return { count: state.count + 1 }; | ||
case "decrement": | ||
return { count: state.count - 1 }; | ||
default: | ||
throw new Error(); | ||
} | ||
} | ||
|
||
const [store, dispatch] = createReducer(reducer, initialState); | ||
|
||
function Counter() { | ||
const [state, setState] = useState(() => store.getState()); | ||
useEffect(()=>{ | ||
store.addListener(setState); | ||
return ()=>{ | ||
store.removeListener(setState); | ||
}; | ||
}, []); | ||
return ( | ||
<> | ||
Count: {state.count} | ||
<button onClick={() => dispatch({type: 'decrement'})}>-</button> | ||
<button onClick={() => dispatch({type: 'increment'})}>+</button> | ||
</> | ||
); | ||
const [state, setState] = useState(() => store.getState()); | ||
useEffect(() => { | ||
store.addListener(setState); | ||
return () => { | ||
store.removeListener(setState); | ||
}; | ||
}, []); | ||
|
||
return ( | ||
<> | ||
Count: {state.count} | ||
<button onClick={() => dispatch({ type: "decrement" })}>-</button> | ||
<button onClick={() => dispatch({ type: "increment" })}>+</button> | ||
</> | ||
); | ||
} | ||
``` | ||
|
||
## Codesandbox | ||
|
||
https://codesandbox.io/s/reshow-flux-base-34umk |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,13 @@ | ||
{ | ||
"version": "0.18.2", | ||
"version": "1.0.0", | ||
"name": "reshow-flux-base", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/react-atomic/reshow", | ||
"directory": "packages/reshow-flux-base" | ||
}, | ||
"homepage": "https://github.com/react-atomic/reshow/tree/main/packages/reshow-flux-base", | ||
"description": "Pure flux dispatch mechanism", | ||
"description": "Simplify state management with no dependencies, yet powerful.", | ||
"keywords": [ | ||
"state", | ||
"global-state", | ||
|
@@ -18,12 +18,6 @@ | |
], | ||
"author": "Hill <[email protected]>", | ||
"license": "ISC", | ||
"dependencies": { | ||
"call-func": "*", | ||
"get-object-value": "*", | ||
"reshow-constant": "*", | ||
"reshow-runtime": "*" | ||
}, | ||
"devDependencies": { | ||
"@babel/cli": "^7.x", | ||
"reshow-unit-dom": "*" | ||
|
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.