Skip to content

Commit 3e4b127

Browse files
authored
Merge pull request #6 from react-gx/dev
Add new hooks useOperations
2 parents 8702376 + 19bdceb commit 3e4b127

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

85 files changed

+372
-570
lines changed

.npmignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
node_modules
22
src
3-
dist
43
tsconfig.json

README.md

Lines changed: 84 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@
66
[![npm downloads](https://img.shields.io/npm/dm/%40dilane3%2Fgx)](https://www.npmjs.com/package/@dilane3/gx)
77
[![GitHub license](https://img.shields.io/github/license/react-gx/gx)](https://github.com/react-gx/gx/blob/main/LICENSE)
88

9-
109
![logo](https://lh4.googleusercontent.com/k2V9Oh-tfABeDjwovtMUqE-lt6cULH0c1EFgb-XNTFh1lt5DVGTGhl3Ty3fMF3xhCBY=w2400)
1110

12-
1311
This library aims to provide you an `easy` and `fast` way to set up and manage the global state of your **`react`** application.
1412

1513
## Documentation
@@ -48,7 +46,7 @@ import React, { StrictMode } from "react";
4846
function App() {
4947
return (
5048
<StrictMode>
51-
{
49+
{
5250
// Your application here
5351
}
5452
</StrictMode>
@@ -66,7 +64,7 @@ import React, { Fragment } from "react";
6664
function App() {
6765
return (
6866
<Fragment>
69-
{
67+
{
7068
// Your application here
7169
}
7270
</Fragment>
@@ -86,10 +84,9 @@ module.exports = {
8684
};
8785
```
8886

89-
9087
## Definition of concepts
9188

92-
**GX** comes with some new concepts like `signal`, `action` and `store`.
89+
**GX** comes with some new concepts like `signal`, `action`, and `store`.
9390

9491
### 1. Signal
9592

@@ -100,7 +97,7 @@ For handle it, there is a special `createSignal` function for this case.
10097

10198
### 2. Action
10299

103-
**Actions** represent functions that act to the state and make it changing over the time.
100+
**Actions** represent functions that act to the state and make it changing over the time.
104101

105102
Your have to specify these `actions` when you create yours `signals`.
106103

@@ -121,7 +118,6 @@ For structuring your code very well you have to follow these steps.
121118
- Inside the signals directory you will create files that will contains your state declaration with actions that act to this state. (**ie: counter.js**)
122119
- Inside the store directory, just create an index.js file. We will see how to fill it.
123120

124-
125121
Here is the result.
126122

127123
![structure](https://lh3.googleusercontent.com/_z_JTStNFHyXTmjz4GrcphAN6BC_CeKYxN1zwyxWGC-ujpIcVTqthesXT6Lfe8b4t1M=w2400)
@@ -131,10 +127,10 @@ Here is the result.
131127
Inside the `signals` directory, create a file called `counter.js` for example.
132128

133129
```js
134-
import { createSignal } from '@dilane3/gx';
130+
import { createSignal } from "@dilane3/gx";
135131

136132
const counterSignal = createSignal({
137-
name: 'counter',
133+
name: "counter",
138134
state: 0,
139135
actions: {
140136
increment: (state, payload) => {
@@ -143,8 +139,8 @@ const counterSignal = createSignal({
143139

144140
decrement: (state, payload) => {
145141
return state - payload;
146-
}
147-
}
142+
},
143+
},
148144
});
149145

150146
export default counterSignal;
@@ -173,7 +169,7 @@ import GXProvider from "@dilane3/gx";
173169
function App() {
174170
return (
175171
<GXProvider store={store}>
176-
{
172+
{
177173
// Your application here
178174
}
179175
</GXProvider>
@@ -187,7 +183,6 @@ export default App;
187183

188184
Create a component called `Counter` inside the Counter.js file. Then import two hooks from `gx` called `useSignal` and `useActions` like follow.
189185

190-
191186
```js
192187
import React from "react";
193188
import { useSignal, useActions } from "@dilane3/gx";
@@ -214,12 +209,79 @@ function Counter() {
214209
export default Counter;
215210
```
216211

217-
Note that the `useSignal` hook takes the name of the signal as a parameter and return the state contained inside that signal.
212+
Note that the `useSignal` hook takes the name of the signal as a parameter and return the state contained inside that signal.
218213

219214
The `useAction` hook takes the name of the signal too and returns an object that contains all the actions of this signal.
220215

221216
Actually, if you click on the increment button, the counter will increase by one and if you click on the decrement button, the counter will decrease by one.
222217

218+
### Sixth step: Adding operations to your signals.
219+
220+
This feature comes with the version `1.3.0` of `gx`. It allows you to add operations to your signals.
221+
**Operations** are functions that use your current state and apply some filters on it. They return the result of the operation without changing the state.
222+
223+
For example, if you want to know if the counter is even or odd, you can create an operation called `isEven` like follow.
224+
225+
```js
226+
import { createSignal } from "@dilane3/gx";
227+
228+
const counterSignal = createSignal({
229+
name: "counter",
230+
state: 0,
231+
actions: {
232+
increment: (state, payload) => {
233+
return state + payload;
234+
},
235+
236+
decrement: (state, payload) => {
237+
return state - payload;
238+
},
239+
},
240+
241+
// Operations section
242+
operations: {
243+
isEven: (state) => {
244+
return state % 2 === 0;
245+
},
246+
},
247+
});
248+
249+
export default counterSignal;
250+
```
251+
252+
Then, you can use it inside your component like follow.
253+
254+
```js
255+
import React from "react";
256+
import { useSignal, useActions, useOperations } from "@dilane3/gx";
257+
258+
function Counter() {
259+
// State
260+
const counter = useSignal("counter");
261+
262+
// Actions
263+
const { increment, decrement } = useActions("counter");
264+
265+
// Operations
266+
const { isEven } = useOperations("counter");
267+
268+
return (
269+
<div>
270+
<h1>Counter App</h1>
271+
272+
<p>Count: {counter}</p>
273+
274+
<p>is even: {isEven() ? "yes" : "no"}</p>
275+
276+
<button onClick={() => increment(1)}>Increment</button>
277+
<button onClick={() => decrement(1)}>Decrement</button>
278+
</div>
279+
);
280+
}
281+
282+
export default Counter;
283+
```
284+
223285
## API
224286

225287
### `createSignal`
@@ -228,12 +290,11 @@ This function takes an object as a parameter and returns a signal.
228290

229291
The object must contain the following properties:
230292

231-
232-
| Properties | Type | Description |
233-
| --- | --- | --- |
234-
| `name` | `string` | The name of the signal. It must be unique. |
235-
| `state` | `any` | The initial state of the signal. |
236-
| `actions` | `object` | An object that contains all the actions of the signal. |
293+
| Properties | Type | Description |
294+
| ---------- | -------- | ------------------------------------------------------ |
295+
| `name` | `string` | The name of the signal. It must be unique. |
296+
| `state` | `any` | The initial state of the signal. |
297+
| `actions` | `object` | An object that contains all the actions of the signal. |
237298

238299
Structure of the `actions` object:
239300

@@ -242,13 +303,12 @@ Structure of the `actions` object:
242303
actionName: (state, payload) => {
243304
// Do something with the state and the payload
244305
return state;
245-
}
306+
};
246307
}
247308
```
248309

249310
All actions must return the new state of the signal.
250311

251-
252312
### `createStore`
253313

254314
This function takes an array of signals as a parameter and returns a store.
@@ -264,7 +324,7 @@ This component takes a store as a parameter and wraps your application with it.
264324
```jsx
265325
const App = () => (
266326
<GXProvider store={store}>
267-
{
327+
{
268328
// Your application here
269329
}
270330
</GXProvider>

cjs/contexts/index.d.ts

Lines changed: 0 additions & 4 deletions
This file was deleted.

cjs/contexts/index.js

Lines changed: 0 additions & 12 deletions
This file was deleted.

cjs/contexts/index.js.map

Lines changed: 0 additions & 1 deletion
This file was deleted.

cjs/contexts/types.d.ts

Lines changed: 0 additions & 19 deletions
This file was deleted.

cjs/contexts/types.js

Lines changed: 0 additions & 6 deletions
This file was deleted.

cjs/contexts/types.js.map

Lines changed: 0 additions & 1 deletion
This file was deleted.

cjs/helpers/createSignal.d.ts

Lines changed: 0 additions & 13 deletions
This file was deleted.

cjs/helpers/createSignal.js

Lines changed: 0 additions & 31 deletions
This file was deleted.

0 commit comments

Comments
 (0)