react-native-global-state-hooks 6.0.0 → 6.0.1

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
@@ -32,6 +32,29 @@ return <Button onClick={() => setCount((count) => count + 1)}>{count}</Button>;
32
32
 
33
33
  Isn't it cool? It works just like a regular **useState**. Notice the only difference is that now you don't need to provide the initial value since this is a global hook, and the initial value has already been provided.
34
34
 
35
+ # Using async persist storage
36
+
37
+ Just add a key for the async storage
38
+
39
+ ```ts
40
+ const useCountPersisted = createGlobalState(1, {
41
+ asyncStorage: {
42
+ key: "count",
43
+ },
44
+ });
45
+ ```
46
+
47
+ After this the metadata of the hooks will now include a flag which will help you to determinate if the async storage was already reached.
48
+
49
+ ```ts
50
+ const [count, setCount, { isAsyncStorageReady }] = useCountPersisted();
51
+ ```
52
+
53
+ If no key is provided, the default metadata is null. Otherwise, it's set to **{ isAsyncStorageReady: false }**.
54
+
55
+ Upon the first successful retrieval from **AsyncStorage**, components will re-render with **{ isAsyncStorageReady: true }** in the metadata.
56
+ The metadata and components will be updated and re-rendered even if there's no difference between the value stored in **AsyncStorage** and the store's default value. This is the only instance where the metadata forces a re-render, after this the metadata will not update the component
57
+
35
58
  # Selectors
36
59
 
37
60
  What if you already have a global state that you want to subscribe to, but you don't want your component to listen to all the changes of the state, only a small portion of it? Let's create a more complex **state**
@@ -469,6 +492,28 @@ In the example the hook will work the same and you'll have access to the correct
469
492
 
470
493
  # Extending Global Hooks
471
494
 
495
+ ## `[IMPORTANT!]`: From version 6.0.0, you can continue creating your custom implementations or using your previous ones. However, now AsyncStorage is already integrated into the global hooks with @react-native-async-storage/async-storage. You simply need to add a key for the persistent storage, and that will do the trick.
496
+
497
+ ```ts
498
+ // this is all you need fo using async storage
499
+ const useCountPersisted = createGlobalState(1, {
500
+ asyncStorage: {
501
+ key: "count",
502
+ },
503
+ });
504
+
505
+ /**
506
+ * Usage in your components:
507
+ * [NOTE]: If no key is provided, the default metadata is null. Otherwise, it's set to { isAsyncStorageReady: false }.
508
+ *
509
+ * Upon the first successful retrieval from AsyncStorage, components will re-render with { isAsyncStorageReady: true } in the metadata.
510
+ * The metadata and components will be updated and re-rendered even if there's no difference between the value stored in AsyncStorage and the store's default value. This is the only instance where the metadata forces a re-render, after this the metadata will not update the component
511
+ */
512
+ const [count, setCount, { isAsyncStorageReady }] = useCountPersisted();
513
+ ```
514
+
515
+ ##### Now lets continue analizing how to create a custom GlobalStore!
516
+
472
517
  Creating a global hook that connects to an asyncStorage is made incredibly easy with the **createCustomGlobalState** function.
473
518
 
474
519
  This function returns a new global state builder wrapped with the desired custom implementation, allowing you to get creative! Le'ts see and example:
@@ -49,7 +49,7 @@ export type StoreTools<TState = any, TMetadata = any, TActions = any> = Omit<Glo
49
49
  * @returns {ActionCollectionConfig<TState, TMetadata>} result - The action collection configuration
50
50
  */
51
51
  export interface ActionCollectionConfig<TState, TMetadata = null> {
52
- [key: string]: (...parameters: any[]) => (storeTools: StoreTools<TState, TMetadata>) => unknown | void;
52
+ [key: string]: (...parameters: any[]) => (storeTools: any) => unknown | void;
53
53
  }
54
54
  /**
55
55
  * This is the actions object returned by the hook when you pass an storeActionsConfig configuration
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-global-state-hooks",
3
- "version": "6.0.0",
3
+ "version": "6.0.1",
4
4
  "description": "This is a package to easily handling global-state across your react-native-components No-redux... The library now includes @react-native-async-storage/async-storage to persist your state across sessions... if you want to keep using the old version without async-storage or react-native dependencies just use version 5.0.15 or user react-hooks-global-states instead",
5
5
  "main": "lib/bundle.js",
6
6
  "types": "lib/src/index.d.ts",