Skip to content

Commit 77fc6b1

Browse files
committed
v0.1.0
1 parent 15c3fe5 commit 77fc6b1

14 files changed

+670
-0
lines changed

.babelrc

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"presets": [
3+
"@babel/preset-typescript",
4+
"@babel/preset-react",
5+
[
6+
"@babel/env",
7+
{
8+
"modules": false
9+
}
10+
]
11+
]
12+
}

.eslintrc

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"extends": "airbnb-typescript",
3+
"plugins": [
4+
"react-hooks"
5+
],
6+
"rules": {
7+
"@typescript-eslint/no-explicit-any": 0,
8+
"react/destructuring-assignment": 0,
9+
"no-plusplus": 0,
10+
"jest/valid-expect": 0,
11+
"class-methods-use-this": 0,
12+
"import/prefer-default-export": 0,
13+
"jsx-a11y/no-static-element-interactions": 0,
14+
"jsx-a11y/click-events-have-key-events": 0,
15+
"react-hooks/rules-of-hooks": "error",
16+
"react-hooks/exhaustive-deps": "warn",
17+
"import/no-duplicates": 0
18+
},
19+
"env": {
20+
"browser": true,
21+
"node": true,
22+
},
23+
"overrides": [
24+
{
25+
"files": [
26+
"**/*.test.ts",
27+
"**/*.test.tsx"
28+
],
29+
"env": {
30+
"jest": true
31+
}
32+
}
33+
]
34+
}

.gitignore

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
node_modules
2+
dist
3+
.rpt2_cache
4+
types
5+
coverage
6+
package-lock.json

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Changelog
2+
3+
# 0.1.0
4+
5+
- init project

README.md

+168
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
# hook-store
2+
3+
## Use
4+
5+
### install
6+
7+
`npm install hooxjs -S`
8+
9+
### create some store
10+
11+
```javascript
12+
// counterStore.js
13+
import hoox from 'hooxjs'
14+
15+
const state = {
16+
count: 1
17+
}
18+
19+
export const { getHooxState, useHooxState, createContainer } = hoox(state)
20+
21+
// some action
22+
export const up = () => {
23+
const [hooxState, setHooxState] = getHooxState()
24+
return setHooxState({
25+
count: hooxState.count + 1
26+
})
27+
}
28+
29+
// some computed state
30+
export const useDoubleCount = () => {
31+
const [hooxState] = useHooxState()
32+
return hooxState.count * 2
33+
}
34+
```
35+
36+
### use store
37+
38+
```javascript
39+
import React from 'react'
40+
import ReactDom from 'react-dom'
41+
import { useHooxState, useDoubleCount, up } from './counterStore'
42+
43+
function Child() {
44+
const doubleCount = useDoubleCount()
45+
return <div>{doubleCount}</div>
46+
}
47+
48+
function Counter() {
49+
const [hooxState] = useHooxState()
50+
return (
51+
<div>
52+
<div>{hooxState.count}</div>
53+
<div onClick={() => up()} />
54+
<Child />
55+
</div>
56+
)
57+
}
58+
59+
const Container = createContainer(Counter)
60+
61+
ReactDom.render(<Container />, document.getElementById('#root'))
62+
```
63+
64+
## API
65+
66+
### hoox(state)
67+
68+
```javascript
69+
const state = { count: 0 }
70+
71+
export const {
72+
Provider,
73+
getHooxState,
74+
useHooxState,
75+
getResetState,
76+
createContainer
77+
} = hoox(state)
78+
```
79+
80+
### Provider
81+
82+
hooxState will combine the initialState of Provider props
83+
84+
```javascript
85+
function App() {
86+
return <Provider initialState={{ count: 1 }}>
87+
<YourFunctionComponent>
88+
</Provider>
89+
}
90+
```
91+
92+
### createContainer
93+
94+
suger of Provider
95+
96+
hooxState will combine the initialState of createContainer args[1]
97+
98+
```javascript
99+
const App = createContainer(YourFunctionComponent, { count: 2 })
100+
```
101+
102+
### useHooxState
103+
104+
using this api, build your hook
105+
106+
```javascript
107+
export const useDoubleCount = () => {
108+
const [hooxState, setHookSate, resetHooxState] = useHooxState()
109+
const { count } = hooxState
110+
return [count * 2, () => setHookSate({ count: count * 2 })]
111+
}
112+
```
113+
114+
### getHooxState
115+
116+
using this api, build your action
117+
118+
```javascript
119+
export const up = () => {
120+
const [hooxState, setHooxState, resetHooxState] = getHooxState()
121+
return setHooxState({
122+
count: hooxState.count + 1
123+
})
124+
}
125+
```
126+
127+
### setHooxState
128+
129+
it behaves like `setState` of class Components, but no callback
130+
131+
```javascript
132+
// update State
133+
export const updateWithRecordOld = newCount => {
134+
const [oldState, setHooxState] = getHooxState()
135+
return setHooxState({
136+
count: newCount,
137+
oldCount: oldState.count
138+
})
139+
}
140+
141+
// aonther way to use oldState
142+
export const up = (key, value) => {
143+
const [, setHooxState] = getHooxState()
144+
return setHooxState(oldState => ({
145+
count: oldState.count + 1
146+
}))
147+
}
148+
```
149+
150+
### resetHooxState
151+
152+
it behaves like `setState` of `useState` hook
153+
154+
```javascript
155+
// get resetHooxState from getHooxState or useHooxState
156+
export const reset = (key, value) => {
157+
const [, , resetHooxState] = getHooxState()
158+
return resetHooxState({ count: 0 })
159+
}
160+
```
161+
162+
```javascript
163+
// get resetHooxState from getResetState
164+
export const reset = (key, value) => {
165+
const resetHooxState = getResetState()
166+
return resetHooxState({ count: 0 })
167+
}
168+
```

jest.config.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module.exports = {
2+
preset: 'ts-jest',
3+
testEnvironment: 'jsdom'
4+
}

package.json

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
{
2+
"name": "hooxjs",
3+
"version": "0.1.0",
4+
"description": "Functional react state management base on hooks",
5+
"author": "wuomzfx <[email protected]>",
6+
"license": "MIT",
7+
"module": "dist/index.esm.js",
8+
"main": "dist/index.cjs.js",
9+
"scripts": {
10+
"test": "jest",
11+
"cov": "jest --coverage",
12+
"lint": "eslint 'src/**/*.*'",
13+
"build": "tsc && npm run rollup",
14+
"rollup": "rollup -c rollup.config.js",
15+
"prebuild": "rm -rf dist && rm -rf types && npm run ci",
16+
"ci": "npm run lint && npm run test",
17+
"prepublishOnly": "npm run build",
18+
"pub-beta": "npm publish --tag=beta"
19+
},
20+
"repository": {
21+
"type": "git",
22+
"url": "git+ssh://[email protected]/wuomzfx/hoox.git"
23+
},
24+
"keywords": [
25+
"hooks",
26+
"react",
27+
"state",
28+
"store"
29+
],
30+
"peerDependencies": {
31+
"@babel/runtime": "^7.4.4",
32+
"react": "^16.8.6",
33+
"react-dom": "^16.8.6"
34+
},
35+
"devDependencies": {
36+
"@babel/core": "^7.4.4",
37+
"@babel/plugin-transform-runtime": "^7.4.4",
38+
"@babel/preset-env": "^7.4.4",
39+
"@babel/preset-react": "^7.0.0",
40+
"@babel/preset-typescript": "^7.3.3",
41+
"@testing-library/react": "^8.0.7",
42+
"@testing-library/react-hooks": "^1.1.0",
43+
"@types/jest": "^24.0.15",
44+
"@types/react": "^16.8.23",
45+
"@types/react-dom": "^16.8.5",
46+
"@typescript-eslint/eslint-plugin": "^2.2.0",
47+
"eslint": "^6.1.0",
48+
"eslint-config-airbnb-typescript": "^4.0.1",
49+
"eslint-plugin-import": "^2.18.2",
50+
"eslint-plugin-jsx-a11y": "^6.2.3",
51+
"eslint-plugin-react": "^7.14.3",
52+
"eslint-plugin-react-hooks": "^2.0.1",
53+
"jest": "^24.8.0",
54+
"react": "^16.9.0",
55+
"react-dom": "^16.9.0",
56+
"react-test-renderer": "^16.9.0",
57+
"rollup": "^1.12.2",
58+
"rollup-plugin-babel": "^4.3.2",
59+
"rollup-plugin-node-resolve": "^5.2.0",
60+
"ts-jest": "^24.0.2",
61+
"typescript": "^3.5.3"
62+
},
63+
"bugs": {
64+
"url": "https://github.com/wuomzfx/hoox/issues"
65+
},
66+
"homepage": "https://github.com/wuomzfx/hoox#readme",
67+
"directories": {
68+
"test": "test"
69+
}
70+
}

rollup.config.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import babel from 'rollup-plugin-babel';
2+
import resolve from 'rollup-plugin-node-resolve';
3+
4+
export default {
5+
input: './src/index.tsx',
6+
output: [
7+
{
8+
file: './dist/index.cjs.js',
9+
format: 'cjs',
10+
},
11+
{
12+
file: './dist/index.esm.js',
13+
format: 'esm',
14+
},
15+
],
16+
external: id => id.startsWith('@babel/runtime/') || id === 'react',
17+
plugins: [
18+
resolve({
19+
extensions: ['.tsx', '.ts'],
20+
}),
21+
babel({
22+
runtimeHelpers: true,
23+
include: ['src/**/*.*'],
24+
plugins: ['@babel/plugin-transform-runtime'],
25+
extensions: ['.tsx', '.ts'],
26+
}),
27+
],
28+
};

0 commit comments

Comments
 (0)