Skip to content

Commit 6b55c55

Browse files
authored
Some improvements: rename Provider to Store, add types + fix eslint (#14)
* Some improvements: rename Provider to Store, add types + fix eslint * Add linter to action * Change test to it
1 parent a43db5c commit 6b55c55

11 files changed

Lines changed: 651 additions & 494 deletions

File tree

.eslintrc.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,14 @@ module.exports = {
2020
'react',
2121
'react-hooks',
2222
'testing-library',
23-
'jest'
23+
'jest',
2424
],
2525
'rules': {
2626
'require-jsdoc': 0,
27+
'react/prop-types': 0,
2728
'max-len': [
2829
'error',
29-
{ 'code': 95 },
30+
{'code': 80, 'ignoreStrings': true},
3031
],
3132
'no-restricted-imports': [
3233
'error',

.github/workflows/test.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,5 @@ jobs:
2121
run: |
2222
yarn install
2323
yarn build
24+
yarn lint
2425
yarn test

README.md

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -46,23 +46,23 @@ npm install fragstore --save
4646
```
4747

4848
## Usage:
49-
### Provider
49+
### Store
5050

51-
The `Provider` is required for any of its child components to consume the store.
51+
The `Store` is required for any of its child components to consume the store.
5252

5353
```js
5454
import createStore from "fragstore";
5555

56-
const { Provider } = createStore({
56+
const { Store } = createStore({
5757
username: "Aral",
5858
age: 31
5959
});
6060

6161
function App() {
6262
return (
63-
<Provider>
63+
<Store>
6464
{/* rest */}
65-
</Provider>
65+
</Store>
6666
);
6767
}
6868
```
@@ -133,22 +133,22 @@ function UnfragmentedExample() {
133133

134134
There are 3 ways to add a new property to the store:
135135

136-
#### 1. Adding a new property on the Provider
136+
#### 1. Adding a new property on the Store
137137

138138
```js
139139
import createStore from "fragstore";
140140

141-
const { Provider } = createStore({ username: "Aral" });
141+
const { Store } = createStore({ username: "Aral" });
142142

143143
function App() {
144-
return <Provider store={{ count: 0 }}>{/* rest */}</Provider>;
144+
return <Store store={{ count: 0 }}>{/* rest */}</Store>;
145145
}
146146
```
147147

148148
#### 2. Using the `useStore` to consume to a new property
149149

150150
```js
151-
const { Provider } = createStore({ username: "Aral" });
151+
const { Store } = createStore({ username: "Aral" });
152152
// ...
153153
const [newProp, setNewProp] = useStore.newProp("Initial value of newProp")
154154
const [anotherProp, setAnotherProp] = useStore.anotherProp()
@@ -157,10 +157,10 @@ setAnotherProp("Initial value of anotherProp")
157157
setNewProp("Next value of newProp")
158158
```
159159

160-
The hook argument works to define the initial value. It doesn't work when the initial value is already defined in `createStore` or `Provider`:
160+
The hook argument works to define the initial value. It doesn't work when the initial value is already defined in `createStore` or `Store`:
161161

162162
```js
163-
const { Provider } = createStore({ username: "Aral" });
163+
const { Store } = createStore({ username: "Aral" });
164164
// ...
165165
const [username, setUsername] = useStore.username("Another name")
166166
console.log(username) // -> Aral
@@ -220,13 +220,13 @@ const callbacks = {
220220
}
221221
}
222222

223-
const { Provider, useQuantity } = createStore(initialState, callbacks)
223+
const { Store, useQuantity } = createStore(initialState, callbacks)
224224
```
225225

226-
Also you can overwrite or define callbacks on the `Provider`:
226+
Also you can overwrite or define callbacks on the `Store`:
227227

228228
```js
229-
<Provider
229+
<Store
230230
store={{ newProperty: 'Another value'}}
231231
callbacks={{
232232
newProperty(value) {
@@ -240,7 +240,7 @@ Also you can overwrite or define callbacks on the `Provider`:
240240
```js
241241
import createStore from "fragstore";
242242

243-
const { Provider, useStore } = createStore({
243+
const { Store, useStore } = createStore({
244244
username: "Aral",
245245
age: 31,
246246
cart: {
@@ -251,14 +251,14 @@ const { Provider, useStore } = createStore({
251251

252252
export default function App() {
253253
return (
254-
<Provider>
254+
<Store>
255255
<AllStore />
256256
<Username />
257257
<CartPrice />
258258
<CartFirstItem />
259259
<Age />
260260
<NewProperty />
261-
</Provider>
261+
</Store>
262262
);
263263
}
264264

babel.config.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
module.exports = {
22
presets: [
33
[
4-
"@babel/preset-env",
4+
'@babel/preset-env',
55
{
66
shippedProposals: true,
77
targets: {
8-
node: "current",
8+
node: 'current',
99
},
1010
},
1111
],
12-
["@babel/preset-react", { runtime: "automatic" }],
12+
['@babel/preset-react', {runtime: 'automatic'}],
1313
],
1414
plugins: [],
1515
};

package.json

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
{
22
"name": "fragstore",
3-
"version": "0.4.1",
3+
"version": "0.5.0",
44
"description": "Small library to manage the (P)React state",
55
"source": "package/index.js",
66
"main": "dist/index.js",
7-
"module": "dist/index.module.js",
8-
"unpkg": "dist/index.umd.js",
7+
"umd:main": "dist/index.umd.js",
8+
"module": "dist/index.m.js",
9+
"exports": {
10+
"require": "./dist/index.js",
11+
"default": "./dist/index.modern.js"
12+
},
13+
"types": "dist/index.d.ts" ,
914
"scripts": {
10-
"lint": "npx eslint --fix package tests",
15+
"lint": "eslint ./package",
16+
"format": "eslint --fix ./package",
1117
"test": "jest",
1218
"test:watch": "jest --watch",
1319
"build": "microbundle --jsx React.createElement",

package/index.js

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,29 @@ export default function createStore(defaultStore = {}, defaultCallbacks = {}) {
99
let allCallbacks = defaultCallbacks;
1010

1111
/**
12-
* Provider of the store
12+
* Store of the store
1313
*
1414
* @example
1515
*
1616
* // Default usage
17-
* const { Provider } = createStore({ count: 0 })
17+
* const { Store } = createStore({ count: 0 })
1818
* // ...
19-
* <Provider>{children}</Provider>
19+
* <Store>{children}</Store>
2020
*
21-
* // Creating the default store in the Provider
22-
* const { Provider } = createStore()
21+
* // Creating the default store in the Store
22+
* const { Store } = createStore()
2323
* // ...
24-
* <Provider store={{ count: 0 }}>{children}</Provider>
24+
* <Store store={{ count: 0 }}>{children}</Store>
2525
*
2626
* // Defining callbacks
27-
* const { Provider } = createStore({ count: 0 })
27+
* const { Store } = createStore({ count: 0 })
2828
* // ...
29-
* <Provider callbacks={{ count: (v) => console.log(v) }}>
29+
* <Store callbacks={{ count: (v) => console.log(v) }}>
3030
* {children}
31-
* </Provider>
31+
* </Store>
3232
* @return {React.ReactNode} children
3333
*/
34-
function Provider({store = {}, callbacks = {}, children}) {
34+
function Store({store = {}, callbacks = {}, children}) {
3535
const initialized = useRef();
3636

3737
if (!initialized.current) {
@@ -68,7 +68,7 @@ export default function createStore(defaultStore = {}, defaultCallbacks = {}) {
6868
discard: new Set(['prototype', 'isReactComponent']),
6969
get(_, path) {
7070
if (!this.discard.has(path)) this.path.push(path);
71-
return new Proxy(() => {}, validator);
71+
return new Proxy(() => { }, validator);
7272
},
7373
apply(t, _, args) {
7474
const path = this.path.slice();
@@ -102,7 +102,7 @@ export default function createStore(defaultStore = {}, defaultCallbacks = {}) {
102102
return [value, update, resetField(prop)];
103103
},
104104
};
105-
const useStore = new Proxy(() => {}, validator);
105+
const useStore = new Proxy(() => { }, validator);
106106

107107
/**
108108
* Hook to register a listener to force a render when the
@@ -194,12 +194,12 @@ export default function createStore(defaultStore = {}, defaultCallbacks = {}) {
194194
}
195195

196196
/**
197-
* createStore function returns the Provider component with the
197+
* createStore function returns the Store component with the
198198
* useStore hook.
199199
*
200-
* @returns {object} { Provider, useStore }
200+
* @returns {object} { Store, useStore }
201201
*/
202-
return {Provider, useStore};
202+
return {Store, useStore};
203203
}
204204

205205
// ##########################################################

0 commit comments

Comments
 (0)