react-hooks-global-states 1.1.0 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -58,15 +58,67 @@ Now, let's say we want to have a filter bar for the contacts that will only have
58
58
 
59
59
  **FilterBar.tsx**
60
60
 
61
- ```ts
62
- const [{ filter }, setState] = useContacts(({ filter }) => ({ filter }));
63
-
64
- return <TextInput onChangeText={() => setState((state) => ({ ...state, filter }))} />;
61
+ ```tsx
62
+ const [contacts] = useContacts((state) => state.contacts.filter((contact) => contact.status === 'active'));
63
+
64
+ return (
65
+ <ul>
66
+ {contacts.map((contact) => (
67
+ <li key={contact.id}>{contact.name}</li>
68
+ ))}
69
+ </ul>
70
+ );
65
71
  ```
66
72
 
67
73
  There you have it again, super simple! By adding a **selector** function, you are able to create a derivative hook that will only trigger when the result of the **selector** changes.
68
74
 
69
- By the way, in the example, the **selector** returning a new object is not a problem at all. This is because, by default, there is a shallow comparison between the previous and current versions of the state, so the render won't trigger if it's not necessary.
75
+ If you want to have more control over when the hook should recompute the selector result, there are a couple of options:
76
+
77
+ ```tsx
78
+ const [filter, setFilter] = useState('');
79
+
80
+ const [contacts] = useContacts((state) => state.contacts.filter((contact) => contact.name.includes(filter)), {
81
+ /**
82
+ * You can use the `isEqualRoot` to validate if the values before the selector are equal.
83
+ * This validation will run before `isEqual` and if the result is true the selector will not be recomputed.
84
+ * If the result is true the re-render of the component will be prevented.
85
+ */
86
+ isEqualRoot: (r1, r2) => r1.filter === r2.filter,
87
+
88
+ /**
89
+ * You can use the `isEqual` to validate if the values after the selector are equal.
90
+ * This validation will run after the selector computed a new value...
91
+ * and if the result is true it will prevent the re-render of the component.
92
+ */
93
+ isEqual: (filter1, filter2) => filter1 === filter2,
94
+
95
+ /**
96
+ * You can use the `dependencies` array as with regular hooks to to force the recomputation of the selector.
97
+ * Is important ot mention that changes in the dependencies will not trigger a re-render of the component...
98
+ * Instead the recomputation of the selector will returned immediately.
99
+ */
100
+ dependencies: [filter],
101
+ });
102
+
103
+ return (
104
+ <ul>
105
+ {contacts.map((contact) => (
106
+ <li key={contact.id}>{contact.name}</li>
107
+ ))}
108
+ </ul>
109
+ );
110
+ ```
111
+
112
+ If you want to perform a shallow comparison between the previous and new values, you can use the **shallowCompare** function from the library.
113
+
114
+ ```TSX
115
+ ({
116
+ /**
117
+ * You can use the `shallowCompare` from the GlobalStore.utils to compare the values at first level.
118
+ */
119
+ isEqual: shallowCompare,
120
+ })
121
+ ```
70
122
 
71
123
  ## What if you want to reuse the selector?
72
124
 
@@ -288,7 +340,7 @@ What if you have two states and you want to combine them? You may have already g
288
340
 
289
341
  By utilizing the approach of combining **emitters** and **hooks**, you can effectively merge multiple states and make them shareable. This allows for better organization and simplifies the management of the combined states. You don't need to refactor everything; you just need to combine the **global state hooks** you already have. Let's see a simple example:
290
342
 
291
- Fist we are gonna create a couple of **global states**, and extract the **stateRetriever**. (In case you are using an instance of **GlobalStore** or **GlobalStoreAbstract** you can just pick up the stateRetrievers from the **getHookDecoupled** method)
343
+ First we are gonna create a couple of **global states**, and extract the **stateRetriever**. (In case you are using an instance of **GlobalStore** or **GlobalStoreAbstract** you can just pick up the stateRetrievers from the **getHookDecoupled** method)
292
344
 
293
345
  ```ts
294
346
  const useHook1 = createGlobalState({
@@ -408,7 +460,7 @@ You have the freedom to combine as many global hooks as you wish. This means you
408
460
 
409
461
  ### **Quick note**:
410
462
 
411
- Please be aware that the third parameter is a **dispose callback**, which can be particularly useful in **high-order** functions when you want to release any resources associated with the hook. By invoking the dispose callback, the hook will no longer report any changes, ensuring that resources are properly cleaned up. This allows for efficient resource management and can be beneficial in scenarios where you need to handle resource cleanup or termination in a controlled manner.
463
+ Please be aware that the third parameter is a **dispose callback**, which can be particularly useful in **higher-order** functions when you want to release any resources associated with the hook. By invoking the dispose callback, the hook will no longer report any changes, ensuring that resources are properly cleaned up. This allows for efficient resource management and can be beneficial in scenarios where you need to handle resource cleanup or termination in a controlled manner.
412
464
 
413
465
  ## stateMutator
414
466