You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+23-49
Original file line number
Diff line number
Diff line change
@@ -29,7 +29,7 @@ Does _Hookleton_ make your life a little more easy? Consider <a href="https://ww
29
29
- Low Memory Consumption and CPU Usage
30
30
- Very fast, as fast as the React Hook _runtime_
31
31
- Works in any environment that supports React Hook: _React Native_, _React Server-Side Rendering ([next.js](https://github.com/zeit/next.js/))_, _[Proto Native](https://github.com/kusti8/proton-native)_, ...
32
-
- Extensible by advanced users
32
+
- Extensible
33
33
- Very low cognitive load
34
34
35
35
## Install
@@ -45,23 +45,8 @@ The Hookleton package exposes `createHook` function that does all.
45
45
46
46
-`useHook` is the user provide Hook
47
47
-`initial` any params that _useHook_ will accept
48
-
-`useHookleton` returned Hookleton
49
-
50
-
### a single convention
51
-
52
-
By **convention** Hookleton inspect for changes in element **1** of _array_ returned by _useHook_ before notify(_force render_) to users components of the hookleton.
53
-
Make sure you update that value, `hook Output[0]`, if you want the changes to be notified.
54
-
55
-
An example, in case it has not been clear:
56
-
57
-
```javascript
58
-
constuseExample=createHook(() => {
59
-
const [, setState] =useState(0);
60
-
return ['any value', setState];
61
-
});
62
-
```
63
-
64
-
If you do something like this, when `setState` update state the hookleton **host** will be rendered but **Hookleton** will not see any change cause element **1** is a constant value, `any value`, hence the rest of users component will not be rendered.
48
+
-`useHookleton` returned Hookleton. Called by *non-host* components
49
+
-`useHookleton.use` returned Hookleton. Called by **the host** component
65
50
66
51
## usage Example
67
52
@@ -88,11 +73,13 @@ const Decrement = () => {
88
73
return<button onClick={decrement}>-</button>;
89
74
};
90
75
76
+
// The host component
91
77
constValue= () => {
92
-
const [count] =useCounter(0);
78
+
const [count] =useCounter.use(0);
93
79
return<span>{count}</span>;
94
80
};
95
81
82
+
// Value componet must be at the top
96
83
exportdefault () => (
97
84
<div>
98
85
<Value />
@@ -102,20 +89,20 @@ export default () => (
102
89
);
103
90
```
104
91
105
-
The `Value` component is the **host** of `useCounter` hookleton for being the first component of the hierarchy that _use_ it.
106
-
Remember that `useCounter` is **composing** a `useState` which is where all the logic happens. It could have been any Hook that return an **Array**
92
+
The `Value` component is the **host** of `useCounter` hookleton for being the first component of the hierarchy that call `useCounter.use`.
93
+
Remember that `useCounter` is **composing** a `useState` which is where all the logic happens.
94
+
95
+
## a single convention
96
+
Only one component, the `host`, can call created *hookleton*`use` hook and this component must be at the top of components of the hierarchy.
- a _Mouse_ event listener that notify `{x,y}` position to 600 components in Real Time [page](https://bysabi.github.io/hookleton/mouse/) | [source](./example/pages/mouse.js)
104
+
- a _Mouse_ event listener that notify `{x,y}` position to 1200 components in Real Time [page](https://bysabi.github.io/hookleton/mouse/) | [source](./example/pages/mouse.js)
- 5 counters in 25 components. Include a basic `global store`[page](https://bysabi.github.io/hookleton/counters5x5/) | [source](./example/pages/counters5x5.js)
- 5 counters in 400 components. With a custom blocking renderer [page](https://bysabi.github.io/hookleton/counters10x40blocking/) | [source](./example/pages/counters10x40blocking.js)
118
-
- 10 counters in multiple nested context [page](https://bysabi.github.io/hookleton/counterNested/) | [source](./example/pages/counterNested.js)
119
106
- showing Fetched 10 random users data in 16 components [page](https://bysabi.github.io/hookleton/fetch/) | [source](./example/pages/fetch.js)
120
107
121
108
How would it be with React Context vs Hookletons?
@@ -129,10 +116,9 @@ How would it be with React Context vs Hookletons?
129
116
130
117
The idea is very simple. The first time that a user component of the Hook is instantiated the hookleton is created and the result of the call to `user Hook` will be _linked_ to the `host` of the hookleton. The `user Hook` is the one you want to _globalize_.
131
118
The result to the calls to the hookleton in the `host` component will become `The source of truth`. The rest of user components will receive a reference to _The source of truth_ on each **re-render**.
132
-
As we said the `host` of the hookleton is the first component instance that _use_ it or _call_ it. This is important for two reasons:
119
+
As we said the `host` of the hookleton is the first component instance that call `.use` hook. This is important because:
133
120
134
-
- First, initial state of the Hook only can be defined in the `host` or in the creation moment with `createHook`. Any _initial_ value from the rest of components will be ignored.
135
-
- Second, when the host component is unmounted `non-host` will retain the state but you can not update it until a new component that will be designated `Host` is mounted
121
+
- Initial state of the Hook only can be defined in the `host` or in the creation moment with `createHook`. Any _initial_ value from the rest of components will be ignored.
136
122
137
123
This is all that you need about _Hookletons_ before start to use it
138
124
@@ -146,7 +132,7 @@ A cloudy day googling I was looking for the simplest possible alternative to _Re
146
132
147
133
This does not mean that they are badly constructed, just that they were not built on top of React Hook, they are prior to it or do not benefit at all of the **"Hook engine"**
148
134
149
-
Hookleton solves it elegantly. Actually _Hookleton_ does not know anything about the `useHook` that want to be global, it only matters that the hook returns an _array_ of any size, its runtime `interface`. This means that its utility is to share the _interface_ of the hook not only its state.
135
+
Hookleton solves it elegantly. Actually _Hookleton_ does not know anything about the `useHook` that want to be global. This means that its utility is to share the _interface_ of the hook not only its state.
150
136
151
137
### When and Where use it
152
138
@@ -160,19 +146,18 @@ Hookletons can be used in all kinds of projects, both large and small. Remember
160
146
161
147
## Pitfalls
162
148
163
-
1. If element **1** of the returned _array_ does not change, the users of the hookleton will not be notified
164
-
2. There are two ways to set the initial value of the hookleton. When created with `createHook` and when it is called from the host component.
149
+
1. There are two ways to set the initial value of the hookleton. When created with `createHook` and when it is called from the host component.
165
150
- The value passed in `createHook` creation has priority
166
-
- If it is `undefined` in _createHook_, the value set in the `host` component will be used. There is only one _host_ component, **the first one**
167
-
3.`initial` values passed to **non-host** components will be **ignored**
168
-
4. If the host component is **unmounted**, the calls to state `setter` will fail. To ensure that this does not happen, you can create a component that acts as the host of the hookleton or several of them at the top of the component hierarchy, Ex:
151
+
- If it is `undefined` in _createHook_, the value set in the `host` component will be used. There is only one _host_ component.
152
+
2.`initial` values passed to **non-host** components will be **ignored**
153
+
3. If the host component is **unmounted**, the calls to state `setter` will fail. To ensure that this does not happen, you can create a component that acts as the host of the hookleton or several of them at the top of the component hierarchy, Ex:
169
154
170
155
```javascript
171
156
172
157
constHookletons= () => {
173
-
useExample1(0);
174
-
useExample2('one');
175
-
useExample3({ three:3 });
158
+
useExample1.use(0);
159
+
useExample2.use('one');
160
+
useExample3.use({ three:3 });
176
161
returnnull
177
162
}
178
163
@@ -192,17 +177,6 @@ If you compare it with other non-Hook based solutions, I do not know that it's t
192
177
193
178
You can see how the _rerender_ behave in the above provide examples: _Mouse_, _Counters_, and _Fetch data_
194
179
195
-
## advanced users API
196
-
197
-
Hookleton is implemented in an class with lifecycle methods that can be extended or replaced.
198
-
199
-
`import { Hookleton, createHookWithClass } from 'hookleton'`
200
-
201
-
-`Hookleton` it is the class that implements everything
202
-
-`createHookWithClass` is a function for instantiate derived `Hookleton` classes and do some setup.
203
-
204
-
**\*createHook** is a _createHookWithClass_ function
0 commit comments