Skip to content

Commit a89d7e4

Browse files
authored
Merge pull request #4 from Spyna/develop
updated documentation and examples
2 parents d703a8d + 22ba0e9 commit a89d7e4

18 files changed

+405
-199
lines changed

README.md

Lines changed: 75 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,14 @@
1-
# spyna-react-store
1+
# @spyna/react-store
22

33
> React app state management that uses a storage
44
5-
6-
7-
[![NPM](https://img.shields.io/npm/v/spyna-react-store.svg)](https://www.npmjs.com/package/spyna-react-store) [![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)
8-
5+
[![NPM](https://img.shields.io/npm/v/@spyna/react-store.svg)](https://www.npmjs.com/package/@spyna/react-store) [![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)
96
[![CircleCI](https://img.shields.io/circleci/project/github/Spyna/react-store/master.svg?style=flat-square)](https://img.shields.io/circleci/project/github/Spyna/react-store/master.svg?style=flat-square)
10-
11-
[![Bundle Phobia](https://img.shields.io/bundlephobia/minzip/spyna-react-store.svg?style=flat-square)](https://img.shields.io/bundlephobia/minzip/spyna-react-store.svg?style=flat-square)
12-
13-
[![Npm version](https://img.shields.io/npm/v/spyna-react-store.svg?style=flat-square)](https://img.shields.io/npm/v/spyna-react-store.svg?style=flat-square)
14-
15-
[![Npm downloads](https://img.shields.io/npm/dt/spyna-react-store.svg?style=flat-square)](https://img.shields.io/npm/dt/spyna-react-store.svg)
16-
17-
[![React Version](https://img.shields.io/npm/dependency-version/spyna-react-store/peer/react.svg?style=flat-square)](https://img.shields.io/npm/dependency-version/spyna-react-store/peer/react.svg?style=flat-square)
18-
7+
[![Bundle Phobia](https://img.shields.io/bundlephobia/minzip/@spyna/react-store.svg?style=flat-square)](https://img.shields.io/bundlephobia/minzip/@spyna/react-store.svg?style=flat-square)
8+
[![Npm version](https://img.shields.io/npm/v/@spyna/react-store.svg?style=flat-square)](https://img.shields.io/npm/v/@spyna/react-store.svg?style=flat-square)
9+
[![Npm downloads](https://img.shields.io/npm/dt/@spyna/react-store.svg?style=flat-square)](https://img.shields.io/npm/dt/@spyna/react-store.svg)
10+
[![React Version](https://img.shields.io/npm/dependency-version/@spyna/react-store/peer/react.svg?style=flat-square)](https://img.shields.io/npm/dependency-version/@spyna/react-store/peer/react.svg?style=flat-square)
1911
[![codecov](https://codecov.io/gh/Spyna/react-store/branch/master/graph/badge.svg)](https://codecov.io/gh/Spyna/react-store)
20-
2112
[![codefactor](https://www.codefactor.io/repository/github/Spyna/react-store/badge?style=flat-square)](https://www.codefactor.io/repository/github/spyna/react-store/overview/master)
2213

2314

@@ -29,39 +20,29 @@ https://spyna.github.io/react-store/
2920
## Install
3021

3122
```bash
32-
npm install --save spyna-react-store
23+
npm install --save @spyna/react-store
3324
```
3425

3526
## Usage
3627

37-
TODO
28+
29+
### Create store
3830

3931
```jsx
32+
// App.js
4033
import React, { Component } from 'react'
41-
import { createStore, withStore } from 'spyna-react-store'
42-
43-
class MyComponent extends Component {
44-
render() {
45-
return (
46-
<p>My Store: {this.props.store.amount}</p>
47-
)
48-
}
49-
}
50-
51-
const ConnectedComponent = withStore(MyComponent);
34+
import { createStore } from '@spyna/react-store'
5235

5336
class App extends Component {
5437
render() {
5538
return (
5639
<div>
5740
<h1>My App</h1>
58-
<p>
59-
Uses a store to share data between different Components, Routes,
60-
Views, etc...
61-
</p>
62-
<div className="container">
63-
<ConnectedComponent />
64-
</div>
41+
{/*
42+
children here
43+
<ConnectedComponent />
44+
*/}
45+
6546
</div>
6647
)
6748
}
@@ -75,10 +56,68 @@ const initialValue = {
7556
}
7657
}
7758

78-
export default createStore(initialValue)(App)
59+
export default createStore(App, initialValue)
60+
```
61+
62+
You can pass the *initial store value* as the second argumento of the function `createStore`.
63+
64+
### connect a component to the store
65+
66+
```jsx
67+
// MyComponent
68+
import React, { Component } from 'react'
69+
import { withStore } from '@spyna/react-store'
70+
71+
class MyComponent extends Component {
72+
render() {
73+
return (
74+
<p>My Amount: {this.props.store.get('amount')}</p>
75+
)
76+
}
77+
}
78+
79+
const ConnectedComponent = withStore(MyComponent);
80+
```
81+
82+
### Set data in store
83+
84+
```jsx
85+
this.props.store.set('a_key', 'a value')
86+
this.props.store.set('another_key', {name: 'another value'})
87+
```
88+
89+
### Read data from the store
7990

91+
```jsx
92+
const a_key = this.props.store.get('a_key')
93+
const another_key = this.props.store.get('another_key')
8094
```
8195

96+
### Remove data from the store
97+
98+
```jsx
99+
this.props.store.remove('a_key', 'a value')
100+
```
101+
102+
### Get all data from the store
103+
104+
```jsx
105+
const store = this.props.store.getState()
106+
```
107+
108+
## Contributing
109+
110+
Pull request and contributions are welcome.
111+
The `develop` branch is the one where you want to develope your changes.
112+
The `master` branch is the source code of the current release.
113+
The `gh-pages` branch is mainteined by CI and contains the documentation and example. You don't need to use it.
114+
115+
116+
* Add test for your changes
117+
* Add documentation and examples of your changes under the folder `example`
118+
* Run `yarn prettier` or `npm run prettier` to format the source of the project
119+
* Thank you
120+
82121
## License
83122

84123
MIT © [Spyna](https://github.com/Spyna)

example/package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
{
2-
"name": "spyna-react-store-example",
2+
"name": "@spyna/react-store-example",
33
"homepage": "https://Spyna.github.io/react-store",
44
"version": "0.0.0",
55
"license": "MIT",
66
"private": true,
77
"dependencies": {
8+
"@spyna/react-store": "link:..",
9+
"axios": "^0.18.0",
10+
"prismjs": "^1.15.0",
811
"prop-types": "^15.6.2",
912
"react": "^16.4.1",
1013
"react-dom": "^16.4.1",
11-
"react-scripts": "^1.1.4",
12-
"spyna-react-store": "link:.."
14+
"react-scripts": "^1.1.4"
1315
},
1416
"scripts": {
1517
"start": "react-scripts start",

example/public/index.html

Lines changed: 68 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,79 @@
99
<meta name="theme-color" content="#000000" />
1010

1111
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
12+
<link
13+
href="https://fonts.googleapis.com/icon?family=Material+Icons"
14+
rel="stylesheet"
15+
/>
16+
<link
17+
rel="stylesheet"
18+
href="https://fonts.googleapis.com/css?family=Open+Sans:300,400,600,700"
19+
/>
20+
<link
21+
rel="stylesheet"
22+
href="https://unpkg.com/mustard-ui@latest/dist/css/mustard-ui.min.css"
23+
/>
24+
<link
25+
rel="stylesheet"
26+
href="https://cdnjs.cloudflare.com/ajax/libs/normalize/8.0.0/normalize.min.css"
27+
/>
28+
29+
<link
30+
rel="stylesheet"
31+
href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.13.0/themes/prism.min.css"
32+
/>
1233

13-
<title>spyna-react-store</title>
34+
<link
35+
rel="stylesheet"
36+
href="https://use.fontawesome.com/releases/v5.0.9/css/solid.css"
37+
/>
38+
<link
39+
rel="stylesheet"
40+
href="https://use.fontawesome.com/releases/v5.0.9/css/fontawesome.css"
41+
/>
42+
43+
<title>@spyna/react-store</title>
1444
</head>
1545

1646
<body>
17-
<h1>React shared store</h1>
47+
<section>
48+
<div class="panel">
49+
<div class="panel-head">
50+
<p class="panel-title">@spyna/react-store <a href="https://github.com/Spyna/react-store">View on GitHub</a></p>
51+
</div>
52+
<div class="panel-body">
53+
<p>
54+
<a href="https://www.npmjs.com/package/@spyna/react-store" rel="nofollow" class="rich-diff-level-one"><img src="https://camo.githubusercontent.com/c6f2dacadd965745d3fe511bf0b70ff48899fe87/68747470733a2f2f696d672e736869656c64732e696f2f6e706d2f762f407370796e612f72656163742d73746f72652e737667" alt="NPM" data-canonical-src="https://img.shields.io/npm/v/@spyna/react-store.svg" style="max-width:100%;"></a>
55+
<a href="https://standardjs.com" rel="nofollow" class="rich-diff-level-one"><img src="https://camo.githubusercontent.com/58fbab8bb63d069c1e4fb3fa37c2899c38ffcd18/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f636f64655f7374796c652d7374616e646172642d627269676874677265656e2e737667" alt="JavaScript Style Guide" data-canonical-src="https://img.shields.io/badge/code_style-standard-brightgreen.svg" style="max-width:100%;"></a>
56+
<a href="https://circleci.com/gh/Spyna/workflows/react-store" rel="nofollow" class="rich-diff-level-one"><img src="https://camo.githubusercontent.com/ab44337af01686c437ad2c6f5a65c5b84e2fef73/68747470733a2f2f696d672e736869656c64732e696f2f636972636c6563692f70726f6a6563742f6769746875622f5370796e612f72656163742d73746f72652f6d61737465722e7376673f7374796c653d666c61742d737175617265" alt="CircleCI" data-canonical-src="https://img.shields.io/circleci/project/github/Spyna/react-store/master.svg?style=flat-square" style="max-width:100%;"></a>
57+
58+
<a href="https://bundlephobia.com/result?p=@spyna/[email protected]" rel="nofollow" class="rich-diff-level-one"><img src="https://camo.githubusercontent.com/e4fe09f4723077eb76e1a4422105b0d234536e8b/68747470733a2f2f696d672e736869656c64732e696f2f62756e646c6570686f6269612f6d696e7a69702f407370796e612f72656163742d73746f72652e7376673f7374796c653d666c61742d737175617265" alt="Bundle Phobia" data-canonical-src="https://img.shields.io/bundlephobia/minzip/@spyna/react-store.svg?style=flat-square" style="max-width:100%;"></a>
59+
60+
<a href="https://www.npmjs.com/package/@spyna/react-store" rel="nofollow" class="rich-diff-level-one"><img src="https://camo.githubusercontent.com/727dca580c0a91df8989ed41b39d8d3635e02f97/68747470733a2f2f696d672e736869656c64732e696f2f6e706d2f64742f407370796e612f72656163742d73746f72652e7376673f7374796c653d666c61742d737175617265" alt="Npm downloads" data-canonical-src="https://img.shields.io/npm/dt/@spyna/react-store.svg?style=flat-square" style="max-width:100%;"></a>
61+
62+
<a href="https://www.npmjs.com/package/@spyna/react-store" rel="nofollow" class="rich-diff-level-one"><img src="https://camo.githubusercontent.com/c6b45bda4b0c38375a226eceaa4e05e205226a5d/68747470733a2f2f696d672e736869656c64732e696f2f6e706d2f646570656e64656e63792d76657273696f6e2f407370796e612f72656163742d73746f72652f706565722f72656163742e7376673f7374796c653d666c61742d737175617265" alt="React Version" data-canonical-src="https://img.shields.io/npm/dependency-version/@spyna/react-store/peer/react.svg?style=flat-square" style="max-width:100%;"></a>
1863

19-
<p>React app state management that uses a shared storage</p>
64+
<a href="https://codecov.io/gh/Spyna/react-store" rel="nofollow" class="rich-diff-level-one"><img src="https://camo.githubusercontent.com/39c03f0d43b53932417dc8935468ca8dbdaa69fd/68747470733a2f2f636f6465636f762e696f2f67682f5370796e612f72656163742d73746f72652f6272616e63682f6d61737465722f67726170682f62616467652e737667" alt="codecov" data-canonical-src="https://codecov.io/gh/Spyna/react-store/branch/master/graph/badge.svg" style="max-width:100%;"></a>
65+
<a href="https://www.codefactor.io/repository/github/spyna/react-store/overview/master" rel="nofollow" class="rich-diff-level-one"><img src="https://camo.githubusercontent.com/3a66876f30d1a4f5d9e1007ae6bd17af80d5b017/68747470733a2f2f7777772e636f6465666163746f722e696f2f7265706f7369746f72792f6769746875622f5370796e612f72656163742d73746f72652f62616467653f7374796c653d666c61742d737175617265" alt="codefactor" data-canonical-src="https://www.codefactor.io/repository/github/Spyna/react-store/badge?style=flat-square" style="max-width:100%;"></a>
66+
</p>
67+
<p>
68+
This library gives you the ability to manage the state of a React app. It
69+
uses a global store, shared with all the bound components of the
70+
application.
71+
</p>
72+
<p>
73+
The <code>store</code> is created using the function
74+
<code>createStore</code>. This function is similar to the
75+
<em>redux Provider</em>.
76+
</p>
77+
<p>
78+
To bind a Component to the store you use the function
79+
<code>withStore</code>. This function injects in the Component a
80+
<em>prop</em> called <code>store</code>, whereby you can read, write,
81+
and remove data globally within the application.
82+
</p>
83+
</div>
84+
</section>
2085
<div id="root">
2186
<noscript> You need to enable JavaScript to run this app. </noscript>
2287
</div>

example/public/manifest.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"short_name": "spyna-react-store",
3-
"name": "spyna-react-store",
2+
"short_name": "@spyna/react-store",
3+
"name": "@spyna/react-store",
44
"start_url": "./index.html",
55
"display": "standalone",
66
"theme_color": "#000000",

example/src/App.js

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React, { Component } from 'react'
2-
import { createStore } from 'spyna-react-store'
2+
import { createStore } from '@spyna/react-store'
33

44
import DisplayAmount from './DisplayAmount'
55
import IncrementAmount from './IncrementAmount'
@@ -11,14 +11,27 @@ import NotPermittedOperations from './NotPermittedOperations'
1111
class App extends Component {
1212
render() {
1313
return (
14-
<div className="container">
15-
<DisplayAmount />
16-
<IncrementAmount />
17-
<DecrementAmount />
18-
<SetDataInStore />
19-
<GetDataFromStore />
20-
<NotPermittedOperations />
21-
</div>
14+
<section>
15+
<h2>DEMO</h2>
16+
<section className="section-secondary">
17+
<h1 className="h2">createStore(App)</h1>
18+
<p>
19+
The demo demostrate that you can access the data stored in the{' '}
20+
<strong>Store</strong> from any <em>Component</em> in the App. The
21+
boxes you see below are differents <em>React Components</em>.
22+
</p>
23+
<div className="row">
24+
<DisplayAmount />
25+
<div className="col col-md-6">
26+
<IncrementAmount />
27+
<DecrementAmount />
28+
</div>
29+
<SetDataInStore />
30+
<GetDataFromStore />
31+
<NotPermittedOperations />
32+
</div>
33+
</section>
34+
</section>
2235
)
2336
}
2437
}
@@ -31,8 +44,4 @@ const initialValue = {
3144
}
3245
}
3346

34-
const config = {
35-
debug: true
36-
}
37-
38-
export default createStore(App, initialValue, config)
47+
export default createStore(App, initialValue)

example/src/DecrementAmount.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from 'react'
2-
3-
import { withStore } from 'spyna-react-store'
2+
import Card from './layout/Card'
3+
import { withStore } from '@spyna/react-store'
44

55
class DecrementAmount extends React.Component {
66
displayName = 'DecrementAmount'
@@ -12,13 +12,18 @@ class DecrementAmount extends React.Component {
1212

1313
render() {
1414
return (
15-
<div className="component component3">
16-
<h2>{this.displayName}.js</h2>
17-
<button onClick={this.decrement}>Decrement</button>
15+
<Card title={this.displayName} size={12}>
1816
<p>
1917
This component decrements <strong>amount</strong> in store
2018
</p>
21-
</div>
19+
<ul className="card-actions">
20+
<li>
21+
<button className="button-primary" onClick={this.decrement}>
22+
Decrement
23+
</button>
24+
</li>
25+
</ul>
26+
</Card>
2227
)
2328
}
2429
}

example/src/DisplayAmount.js

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,30 @@
11
import React from 'react'
2+
import Card from './layout/Card'
3+
import { withStore } from '@spyna/react-store'
24

3-
import { withStore } from 'spyna-react-store'
4-
5-
class DisplaystoreData extends React.Component {
6-
displayName = 'DisplaystoreData'
5+
class DisplaystoreData extends Card {
6+
displayName = 'DisplayAmount'
77
render() {
88
const { store } = this.props
99
return (
10-
<div className="component component1">
11-
<h2>{this.displayName}.js</h2>
10+
<Card title={this.displayName}>
1211
<p>
1312
<strong>amount</strong>.<br />
1413
<span className="amount">{store.get('amount')}</span>
1514
</p>
1615
<div>
17-
Amount, accessed using
18-
<pre>store.get('amount', 0)</pre>
16+
Amount, accessed using{' '}
17+
<code className="language-javascript">store.get('amount', 0)</code>
1918
</div>
2019
<div>
21-
<h2>store data</h2>
22-
<pre>{JSON.stringify(store.getState(), null, ' ')}</pre>
20+
<h2>All data stored </h2>
21+
<pre className="language-json">
22+
<code className="language-json">
23+
{JSON.stringify(store.getState(), null, ' ')}
24+
</code>
25+
</pre>
2326
</div>
24-
</div>
27+
</Card>
2528
)
2629
}
2730
}

0 commit comments

Comments
 (0)