Skip to content

Commit dc6633c

Browse files
committed
init.
1 parent fb8616b commit dc6633c

File tree

13 files changed

+450
-0
lines changed

13 files changed

+450
-0
lines changed

.editorconfig

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# EditorConfig is awesome: http://EditorConfig.org
2+
3+
# top-most EditorConfig file
4+
root = true
5+
6+
# Unix-style newlines with a newline ending every file
7+
[*]
8+
end_of_line = lf
9+
insert_final_newline = true
10+
11+
# 2 space indentation
12+
[*.{html,scss,js,json,yml}]
13+
indent_style = space
14+
indent_size = 2
15+
16+
[*.{md,markdown}]
17+
trim_trailing_whitespace = false

.eslintrc.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
module.exports = {
2+
env: {
3+
browser: true,
4+
commonjs: true,
5+
es6: true,
6+
node: true
7+
},
8+
extends: [
9+
'xo',
10+
'xo-space',
11+
'xo-react/space'
12+
],
13+
globals: {
14+
jest: 'readonly',
15+
test: 'readonly',
16+
expect: 'readonly',
17+
beforeEach: 'readonly',
18+
afterEach: 'readonly'
19+
},
20+
parserOptions: {
21+
ecmaFeatures: {
22+
jsx: true
23+
},
24+
ecmaVersion: 2018
25+
},
26+
plugins: [
27+
'react'
28+
],
29+
rules: {
30+
'no-eq-null': 0,
31+
eqeqeq: ['error', 'allow-null']
32+
}
33+
};

.gitignore

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# OS X
2+
.DS_Store
3+
4+
# npm
5+
node_modules
6+
package-lock.json
7+
8+
# build
9+
dist/
10+
11+
# test scripts
12+
coverage/
13+
14+
# WebStorm
15+
.idea

README.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,50 @@
11
# react-hooks-shared-state
22
A global state for React with Hooks API.
3+
4+
## Installation
5+
```bash
6+
$ npm install react-hooks-shared-state --save
7+
```
8+
9+
## Live demo
10+
[https://kelp404.github.io/react-hooks-shared-state](https://kelp404.github.io/react-hooks-shared-state)
11+
12+
## Quick start
13+
```js
14+
const {useSharedState} = require('react-hooks-shared-state');
15+
16+
const Component = () => {
17+
const [state, setState] = useSharedState(null, {value: 'hello'});
18+
const onChange = e => {
19+
setState(Object.assign({}, state, {value: e.target.value}));
20+
};
21+
22+
return (
23+
<input value={state.value} onChange={onChange}/>
24+
);
25+
};
26+
```
27+
28+
## Document
29+
### Syntactic sugar
30+
```js
31+
const {useSharedState} = require('react-hooks-shared-state');
32+
```
33+
```js
34+
const sharedState = require('react-hooks-shared-state');
35+
sharedState.useState()
36+
```
37+
`useSharedState` and `sharedState.useState` are the same one.
38+
39+
### useSharedState(path, initialState)
40+
### Parameter `path`
41+
Type: `string`
42+
Required: `optional`
43+
The object path of the state.
44+
When the path is null it will return the state(root).
45+
If you just want to use a part of state in the component, pass the object path.
46+
47+
### Parameter `initialState`
48+
Type: `any`
49+
Required: `optional`
50+
The initial state.

config/default.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = {
2+
server: {
3+
host: 'localhost',
4+
port: 8000
5+
}
6+
};

example/app.js

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
require('@babel/polyfill');
2+
const React = require('react');
3+
const ReactDOM = require('react-dom');
4+
const {useSharedState} = require('../');
5+
6+
const {useEffect} = React;
7+
8+
const FormA = props => {
9+
const [state, setState] = useSharedState('a', 'value a');
10+
11+
const onChange = e => {
12+
setState(e.target.value);
13+
};
14+
15+
useEffect(() => {
16+
console.log(`effect A: ${state}`);
17+
});
18+
19+
return (
20+
<div {...props}>
21+
<h2>Form A</h2>
22+
<form>
23+
<div className="form-row">
24+
<div className="form-group">
25+
<label htmlFor="inputAddress">state.a</label>
26+
<input type="text" className="form-control" value={state} onChange={onChange}/>
27+
</div>
28+
</div>
29+
</form>
30+
</div>
31+
);
32+
};
33+
34+
const FormB = props => {
35+
const [state, setState] = useSharedState('b', 'value b');
36+
const onChange = e => {
37+
setState(e.target.value);
38+
};
39+
40+
useEffect(() => {
41+
console.log(`effect B: ${state}`);
42+
});
43+
44+
return (
45+
<div {...props}>
46+
<h2>Form B</h2>
47+
<form>
48+
<div className="form-row">
49+
<div className="form-group">
50+
<label htmlFor="inputAddress">state.b</label>
51+
<input type="text" className="form-control" value={state} onChange={onChange}/>
52+
</div>
53+
</div>
54+
</form>
55+
</div>
56+
);
57+
};
58+
59+
const FormC = props => {
60+
const [state, setState] = useSharedState();
61+
62+
const onChangeA = e => {
63+
setState(Object.assign({}, state, {a: e.target.value}));
64+
};
65+
66+
const onChangeB = e => {
67+
setState(Object.assign({}, state, {b: e.target.value}));
68+
};
69+
70+
useEffect(() => {
71+
console.log(`effect C: ${JSON.stringify(state)}`);
72+
});
73+
74+
return (
75+
<div {...props}>
76+
<h2>Form C</h2>
77+
<form>
78+
<div className="form-row">
79+
<div className="form-group">
80+
<label htmlFor="inputAddress">state.a</label>
81+
<input type="text" className="form-control" value={state.a} onChange={onChangeA}/>
82+
</div>
83+
<div className="form-group">
84+
<label htmlFor="inputAddress">state.b</label>
85+
<input type="text" className="form-control" value={state.b} onChange={onChangeB}/>
86+
</div>
87+
</div>
88+
</form>
89+
</div>
90+
);
91+
};
92+
93+
ReactDOM.render(
94+
(
95+
<>
96+
<nav className="navbar navbar-expand-lg navbar-light bg-light">
97+
<a className="navbar-brand" href="/react-hooks-shared-state">react-hooks-shared-state</a>
98+
</nav>
99+
<div className="container pt-3">
100+
<div className="row">
101+
<FormA className="col"/>
102+
<FormB className="col"/>
103+
<FormC className="col"/>
104+
</div>
105+
</div>
106+
</>
107+
),
108+
document.getElementById('root')
109+
);

example/index.html

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8"/>
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
6+
<title>react-hooks-shared-state</title>
7+
<link type="text/css" rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css"/>
8+
<link type="text/css" rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.3.1/css/bootstrap.min.css"/>
9+
</head>
10+
<body>
11+
<div id="root">
12+
<p class="text-center text-muted h3" style="padding: 20px 0">
13+
<i class="fa fa-spinner fa-pulse fa-fw"></i> Loading...
14+
</p>
15+
</div>
16+
<a href="https://github.com/kelp404/react-hooks-shared-state" target="_blank"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_gray_6d6d6d.png" alt="Fork me on GitHub"></a>
17+
<script type="text/javascript" src="http://localhost:8001/app.js"></script>
18+
</body>
19+
</html>

example/server.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const http = require('http');
2+
const path = require('path');
3+
const config = require('config');
4+
const express = require('express');
5+
6+
const app = express();
7+
const server = http.createServer(app);
8+
9+
app.use((req, res) => {
10+
res.sendFile(path.join(__dirname, 'index.html'));
11+
});
12+
13+
// Launch server.
14+
server.listen(config.server.port, config.server.host, () => {
15+
const {address, port} = server.address();
16+
console.log(`Server listening at http://${address}:${port}/react-hooks-shared-state`);
17+
});

index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
./example/index.html

index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const SharedState = require('./lib/shared-state');
2+
3+
const sharedState = new SharedState();
4+
5+
exports.setState = sharedState.setState;
6+
exports.setSharedState = sharedState.setState;
7+
exports.useState = sharedState.useState;
8+
exports.useSharedState = sharedState.useState;

0 commit comments

Comments
 (0)