Skip to content

Commit

Permalink
Independence reshow-flux-base support
Browse files Browse the repository at this point in the history
  • Loading branch information
HillLiu committed Mar 27, 2024
1 parent 686f92c commit 6374073
Show file tree
Hide file tree
Showing 20 changed files with 406 additions and 662 deletions.
79 changes: 38 additions & 41 deletions packages/reshow-flux-base/README.md
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
10 changes: 2 additions & 8 deletions packages/reshow-flux-base/package.json
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",
Expand All @@ -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": "*"
Expand Down
91 changes: 0 additions & 91 deletions packages/reshow-flux-base/src/SimpleMap.js

This file was deleted.

51 changes: 0 additions & 51 deletions packages/reshow-flux-base/src/__tests__/SimpleMapTest.js

This file was deleted.

7 changes: 5 additions & 2 deletions packages/reshow-flux-base/src/__tests__/createReducerTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
* @typedef {import("mocha")}
*/

/**
* @typedef {import("../type").ActionObject} ActionObject
*/

import { expect } from "chai";
import * as sinon from "sinon";
import createReducer from "../createReducer";
import { ActionObject } from "../type";
import { createReducer } from "../createReducer";

describe("Test createReducer", () => {
let reducer;
Expand Down
Loading

0 comments on commit 6374073

Please sign in to comment.