react-native-global-state-hooks 8.0.0 → 8.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 +16 -422
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -332,18 +332,16 @@ Additionally, to subscribe to state changes, you can pass a callback function as
|
|
|
332
332
|
* This not only allows you to retrieve the current value of the state...
|
|
333
333
|
* but also enables you to subscribe to any changes in the state or a portion of it
|
|
334
334
|
*/
|
|
335
|
-
const
|
|
336
|
-
|
|
337
|
-
console.log("state changed: ", state);
|
|
338
|
-
});
|
|
339
|
-
|
|
340
|
-
subscribe(
|
|
341
|
-
(state) => state.isLoading,
|
|
342
|
-
(isLoading) => {
|
|
343
|
-
console.log("is loading changed", isLoading);
|
|
344
|
-
}
|
|
345
|
-
);
|
|
335
|
+
const unsubscribe1 = contactsRetriever((state) => {
|
|
336
|
+
console.log("state changed: ", state);
|
|
346
337
|
});
|
|
338
|
+
|
|
339
|
+
const unsubscribe2 = contactsRetriever(
|
|
340
|
+
(state) => state.isLoading,
|
|
341
|
+
(isLoading) => {
|
|
342
|
+
console.log("is loading changed", isLoading);
|
|
343
|
+
}
|
|
344
|
+
);
|
|
347
345
|
```
|
|
348
346
|
|
|
349
347
|
That's great, isn't it? everything stays synchronized with the original state!!
|
|
@@ -494,322 +492,6 @@ const MyComponent = () => {
|
|
|
494
492
|
};
|
|
495
493
|
```
|
|
496
494
|
|
|
497
|
-
# Emitters
|
|
498
|
-
|
|
499
|
-
So, we have seen that we can subscribe a callback to state changes, create **selector hooks** from our global states. Guess what? We can also create derived **emitters** and subscribe callbacks to specific portions of the state. Let's review it:
|
|
500
|
-
|
|
501
|
-
```ts
|
|
502
|
-
const subscribeToFilter = createDerivateEmitter(contactsRetriever, ({ filter }) => ({
|
|
503
|
-
filter,
|
|
504
|
-
}));
|
|
505
|
-
```
|
|
506
|
-
|
|
507
|
-
Cool, it's basically the same, but instead of using the **hook** as a parameter, we just have to use the **stateRetriever** as a parameter, and that will make the magic.
|
|
508
|
-
|
|
509
|
-
Now we are able to add a callback that will be executed every time the state of the **filter** changes.
|
|
510
|
-
|
|
511
|
-
```ts
|
|
512
|
-
const removeFilterSubscription = subscribeToFilter<Subscribe>(({ filter }) => {
|
|
513
|
-
console.log(`The filter value changed: ${filter}`);
|
|
514
|
-
});
|
|
515
|
-
```
|
|
516
|
-
|
|
517
|
-
By default, the callback will be executed once subscribed, using the current value of the state. If you want to avoid this initial call, you can pass an extra parameter to the **subscribe** function.
|
|
518
|
-
|
|
519
|
-
```ts
|
|
520
|
-
const removeFilterSubscription = subscribeToFilter<Subscribe>(
|
|
521
|
-
({ filter }) => {
|
|
522
|
-
console.log(`The filter value changed: ${filter}`);
|
|
523
|
-
},
|
|
524
|
-
{
|
|
525
|
-
skipFirst: true,
|
|
526
|
-
}
|
|
527
|
-
);
|
|
528
|
-
```
|
|
529
|
-
|
|
530
|
-
Also, of course, if you have an exceptional case where you want to derived/selected directly from the current **emitter**, you can add a **selector**. This allows you to fine-tune the emitted values based on your requirements
|
|
531
|
-
|
|
532
|
-
```ts
|
|
533
|
-
const removeFilterSubscription = subscribeToFilter<Subscribe>(
|
|
534
|
-
({ filter }) => filter,
|
|
535
|
-
/**
|
|
536
|
-
* Cause of the selector the filter now is an string
|
|
537
|
-
*/
|
|
538
|
-
(filter) => {
|
|
539
|
-
console.log(`The filter value changed: ${filter}`);
|
|
540
|
-
},
|
|
541
|
-
{
|
|
542
|
-
skipFirst: true,
|
|
543
|
-
/**
|
|
544
|
-
* You can also override the default shallow comparison...
|
|
545
|
-
* or disable it completely by setting the isEqual callback to null.
|
|
546
|
-
*/
|
|
547
|
-
isEqual: (a, b) => a === b,
|
|
548
|
-
// isEqual: null // this will avoid doing a shallow comparison
|
|
549
|
-
}
|
|
550
|
-
);
|
|
551
|
-
```
|
|
552
|
-
|
|
553
|
-
And guess what again? You can also derive emitters from derived emitters without any trouble at all! It works basically the same. Let's see an example:
|
|
554
|
-
|
|
555
|
-
```ts
|
|
556
|
-
const subscribeToItems = createDerivateEmitter(contactsRetriever, ({ items }) => items);
|
|
557
|
-
|
|
558
|
-
const subscribeToItemsLength = createDerivateEmitter(subscribeToItems, (items) => items.length);
|
|
559
|
-
```
|
|
560
|
-
|
|
561
|
-
The examples may seem a little silly, but they allow you to see the incredible things you can accomplish with these **derived states** and **emitters**. They open up a world of possibilities!
|
|
562
|
-
|
|
563
|
-
# Combining stateRetriever
|
|
564
|
-
|
|
565
|
-
What if you have two states and you want to combine them? You may have already guessed it right? ... you can create combined **emitters** and **hooks** from the hook **stateRetriever**.
|
|
566
|
-
|
|
567
|
-
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:
|
|
568
|
-
|
|
569
|
-
First we are gonna create a couple of **global states**, and extract the **stateRetriever**.
|
|
570
|
-
|
|
571
|
-
```ts
|
|
572
|
-
const useHook1 = createGlobalState({
|
|
573
|
-
propA: 1,
|
|
574
|
-
propB: 2,
|
|
575
|
-
});
|
|
576
|
-
|
|
577
|
-
const [stateRetriever1, stateMutator1] = useHook1.stateControls();
|
|
578
|
-
|
|
579
|
-
const useHook2 = createGlobalState({
|
|
580
|
-
propC: 3,
|
|
581
|
-
propD: 4,
|
|
582
|
-
});
|
|
583
|
-
|
|
584
|
-
const [, stateRetriever2] = useHook2.stateControls();
|
|
585
|
-
```
|
|
586
|
-
|
|
587
|
-
Okay, cool, the first state as **propA, propB** while the second one has **propC, propD**, let's combine them:
|
|
588
|
-
|
|
589
|
-
```ts
|
|
590
|
-
const [useCombinedHook, combinedStateRetriever] = combineAsyncGetters(
|
|
591
|
-
{
|
|
592
|
-
selector: ([state1, state2]) => ({
|
|
593
|
-
...state1,
|
|
594
|
-
...state2,
|
|
595
|
-
}),
|
|
596
|
-
},
|
|
597
|
-
stateRetriever1,
|
|
598
|
-
stateRetriever2
|
|
599
|
-
);
|
|
600
|
-
```
|
|
601
|
-
|
|
602
|
-
Well, that's it! Now you have access to a **combinedStateRetriever** that will return the combined value of the two states. From this new **combinedStateRetriever**, you can retrieve the value or subscribe to its changes. Let'see:
|
|
603
|
-
|
|
604
|
-
```ts
|
|
605
|
-
const value = stateRetriever(); // { propA, propB, propC, propD }
|
|
606
|
-
|
|
607
|
-
// subscribe to the new emitter
|
|
608
|
-
const unsubscribeGroup = stateRetriever<Subscribe>((subscribe) => {
|
|
609
|
-
subscribe((state) => {
|
|
610
|
-
console.log(subscribe); // full state
|
|
611
|
-
});
|
|
612
|
-
|
|
613
|
-
// Please note that if you add a selector,
|
|
614
|
-
// the callback will only trigger if the result of the selector changes.
|
|
615
|
-
subscribe(
|
|
616
|
-
({ propA, propD }) => ({ propA, propD }),
|
|
617
|
-
(derived) => {
|
|
618
|
-
console.log(derived); // { propA, propD }
|
|
619
|
-
}
|
|
620
|
-
);
|
|
621
|
-
});
|
|
622
|
-
```
|
|
623
|
-
|
|
624
|
-
Regarding the newly created hook, **useCombinedHook**, you can seamlessly utilize it across all your components, just like your other **global state hooks**. This enables a consistent and familiar approach for accessing and managing the combined state within your application.
|
|
625
|
-
|
|
626
|
-
```ts
|
|
627
|
-
const [combinedState] = useCombinedHook();
|
|
628
|
-
```
|
|
629
|
-
|
|
630
|
-
The main difference with **combined hooks** compared to individual **global state hooks** is the absence of **metadata** and **actions**. Instead, combined hooks provide a condensed representation of the underlying global states using simple React functionality. This streamlined approach ensures lightweight usage, making it easy to access and manage the combined state within your components.
|
|
631
|
-
|
|
632
|
-
### Let's explore some additional examples.
|
|
633
|
-
|
|
634
|
-
Similar to your other **global state hooks**, **combined hooks** allow you to use **selectors** directly from consumer components. This capability eliminates the need to create an excessive number of reusable hooks if they are not truly necessary. By utilizing selectors, you can efficiently extract specific data from the **combined state** and utilize it within your components. This approach offers a more concise and focused way of accessing the required state values without the need for creating additional hooks unnecessarily.
|
|
635
|
-
|
|
636
|
-
```ts
|
|
637
|
-
const [fragment] = useCombinedHook(({ propA, propD }) => ({ propA, propD }));
|
|
638
|
-
```
|
|
639
|
-
|
|
640
|
-
Lastly, you have the flexibility to continue combining stateRetrievers if desired. This means you can extend the functionality of combined hooks by adding more stateRetrievers to merge additional states. By combining stateRetrievers in this way, you can create a comprehensive and unified representation of the combined states within your application. This approach allows for modular and scalable state management, enabling you to efficiently handle complex state compositions.
|
|
641
|
-
|
|
642
|
-
Let's see an example:
|
|
643
|
-
|
|
644
|
-
```ts
|
|
645
|
-
const [useCombinedHook, combinedStateRetriever1] = combineAsyncGetters(
|
|
646
|
-
{
|
|
647
|
-
selector: ([state1, state2]) => ({
|
|
648
|
-
...state1,
|
|
649
|
-
...state2,
|
|
650
|
-
}),
|
|
651
|
-
},
|
|
652
|
-
stateRetriever1,
|
|
653
|
-
stateRetriever2
|
|
654
|
-
);
|
|
655
|
-
|
|
656
|
-
const useHook3 = createGlobalState({
|
|
657
|
-
propE: 1,
|
|
658
|
-
propF: 2,
|
|
659
|
-
});
|
|
660
|
-
|
|
661
|
-
const [stateRetriever3, stateMutator3] = useHook3.stateControls();
|
|
662
|
-
|
|
663
|
-
const useIsLoading = createGlobalState(false);
|
|
664
|
-
|
|
665
|
-
const [isLoadingStateRetriever, isLoadingMutator] = useIsLoading.stateControls();
|
|
666
|
-
```
|
|
667
|
-
|
|
668
|
-
Once we created another peace of state, we can combine it with our other **global hooks** and **emitters**
|
|
669
|
-
|
|
670
|
-
```ts
|
|
671
|
-
const [useCombinedHook2, combinedStateRetriever2] = combineAsyncGetters(
|
|
672
|
-
{
|
|
673
|
-
selector: ([state1, state2, isLoading]) => ({
|
|
674
|
-
...state1,
|
|
675
|
-
...state2,
|
|
676
|
-
isLoading,
|
|
677
|
-
}),
|
|
678
|
-
},
|
|
679
|
-
combinedStateRetriever1,
|
|
680
|
-
stateRetriever3,
|
|
681
|
-
isLoadingStateRetriever
|
|
682
|
-
);
|
|
683
|
-
```
|
|
684
|
-
|
|
685
|
-
You have the freedom to combine as many global hooks as you wish. This means you can merge multiple states into a single cohesive unit by combining their respective hooks. This approach offers flexibility and scalability, allowing you to handle complex state compositions in a modular and efficient manner.
|
|
686
|
-
|
|
687
|
-
### **Quick note**:
|
|
688
|
-
|
|
689
|
-
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.
|
|
690
|
-
|
|
691
|
-
# Extending Global Hooks
|
|
692
|
-
|
|
693
|
-
## `[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.
|
|
694
|
-
|
|
695
|
-
**`BUT!`** If you are against using **@react-native-async-storage/async-storage**, you can always use the base library [**react-hooks-global-state**](https://www.npmjs.com/package/react-hooks-global-states), which provides all the global hooks functionality without the persistent storage options. Take a look at the documentation—there's even an example of how to implement your own persistent storage in your own way.
|
|
696
|
-
|
|
697
|
-
```ts
|
|
698
|
-
// this is all you need fo using async storage
|
|
699
|
-
const useCountPersisted = createGlobalState(1, {
|
|
700
|
-
asyncStorage: {
|
|
701
|
-
key: "count",
|
|
702
|
-
},
|
|
703
|
-
});
|
|
704
|
-
|
|
705
|
-
/**
|
|
706
|
-
* Usage in your components:
|
|
707
|
-
* [NOTE]: If no key is provided, the default metadata is null. Otherwise, it's set to { isAsyncStorageReady: false }.
|
|
708
|
-
*
|
|
709
|
-
* Upon the first successful retrieval from AsyncStorage, components will re-render with { isAsyncStorageReady: true } in the metadata.
|
|
710
|
-
* 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
|
|
711
|
-
*/
|
|
712
|
-
const [count, setCount, { isAsyncStorageReady }] = useCountPersisted();
|
|
713
|
-
```
|
|
714
|
-
|
|
715
|
-
##### Now lets continue analyzing how to create a custom GlobalStore!
|
|
716
|
-
|
|
717
|
-
Creating a global hook that connects to an asyncStorage is made incredibly easy with the **createCustomGlobalState** function.
|
|
718
|
-
|
|
719
|
-
This function returns a new global state builder wrapped with the desired custom implementation, allowing you to get creative! Le'ts see and example:
|
|
720
|
-
|
|
721
|
-
```ts
|
|
722
|
-
import { formatFromStore, formatToStore, createCustomGlobalState } = 'react-native-global-state-hooks'
|
|
723
|
-
|
|
724
|
-
// Optional configuration available for the consumers of the builder
|
|
725
|
-
type HookConfig = {
|
|
726
|
-
asyncStorageKey?: string;
|
|
727
|
-
};
|
|
728
|
-
|
|
729
|
-
// This is the base metadata that all the stores created from the builder will have.
|
|
730
|
-
type BaseMetadata = {
|
|
731
|
-
isAsyncStorageReady?: boolean;
|
|
732
|
-
};
|
|
733
|
-
|
|
734
|
-
export const createGlobalState = createCustomGlobalState<
|
|
735
|
-
BaseMetadata,
|
|
736
|
-
HookConfig
|
|
737
|
-
>({
|
|
738
|
-
/**
|
|
739
|
-
* This function executes immediately after the global state is created, before the invocations of the hook
|
|
740
|
-
*/
|
|
741
|
-
onInitialize: async ({ setState, setMetadata }, config) => {
|
|
742
|
-
setMetadata((metadata) => ({
|
|
743
|
-
...(metadata ?? {}),
|
|
744
|
-
isAsyncStorageReady: null,
|
|
745
|
-
}));
|
|
746
|
-
|
|
747
|
-
const asyncStorageKey = config?.asyncStorageKey;
|
|
748
|
-
if (!asyncStorageKey) return;
|
|
749
|
-
|
|
750
|
-
const storedItem = (await asyncStorage.getItem(asyncStorageKey)) as string;
|
|
751
|
-
|
|
752
|
-
// update the metadata, remember, metadata is not reactive
|
|
753
|
-
setMetadata((metadata) => ({
|
|
754
|
-
...metadata,
|
|
755
|
-
isAsyncStorageReady: true,
|
|
756
|
-
}));
|
|
757
|
-
|
|
758
|
-
if (storedItem === null) {
|
|
759
|
-
return setState((state) => state, { forceUpdate: true });
|
|
760
|
-
}
|
|
761
|
-
|
|
762
|
-
const parsed = formatFromStore(storedItem, {
|
|
763
|
-
jsonParse: true,
|
|
764
|
-
});
|
|
765
|
-
|
|
766
|
-
setState(parsed, { forceUpdate: true });
|
|
767
|
-
},
|
|
768
|
-
|
|
769
|
-
onChange: ({ getState }, config) => {
|
|
770
|
-
if (!config?.asyncStorageKey) return;
|
|
771
|
-
|
|
772
|
-
const state = getState();
|
|
773
|
-
|
|
774
|
-
const formattedObject = formatToStore(state, {
|
|
775
|
-
stringify: true,
|
|
776
|
-
});
|
|
777
|
-
|
|
778
|
-
asyncStorage.setItem(config.asyncStorageKey, formattedObject);
|
|
779
|
-
},
|
|
780
|
-
});
|
|
781
|
-
```
|
|
782
|
-
|
|
783
|
-
It is important to use **forceUpdate** to force React to re-render our components and obtain the most recent state of the **metadata**. This is especially useful when working with primitive types, as it can be challenging to differentiate between a primitive value that originates from storage and one that does not.
|
|
784
|
-
|
|
785
|
-
It is worth mentioning that the **onInitialize** function will be executed only once per global state.
|
|
786
|
-
|
|
787
|
-
You can use to **formatToStore**, and **formatFromStore** to sanitize your data, These methods will help you transform objects into JSON strings and retrieve them back without losing any of the original data types. You will no longer encounter problems when **stringifying** Dates, Maps, Sets, and other complex data types. You could take a look in the API here: [json-storage-formatter](https://www.npmjs.com/package/json-storage-formatter).
|
|
788
|
-
|
|
789
|
-
Let's see how to create a global state using our new builder:
|
|
790
|
-
|
|
791
|
-
```ts
|
|
792
|
-
const useTodos = createGlobalState(new Map<string, number>(), {
|
|
793
|
-
config: {
|
|
794
|
-
asyncStorageKey: "todos",
|
|
795
|
-
},
|
|
796
|
-
});
|
|
797
|
-
```
|
|
798
|
-
|
|
799
|
-
That's correct! If you add an **asyncStorageKey** to the state configuration, the state will be synchronized with the **asyncStorage**
|
|
800
|
-
|
|
801
|
-
Let's see how to use this async storage hook into our components:
|
|
802
|
-
|
|
803
|
-
```ts
|
|
804
|
-
const [todos, setTodos, metadata] = useTodos();
|
|
805
|
-
|
|
806
|
-
return (<>
|
|
807
|
-
{metadata.isAsyncStorageReady ? <TodoList todos={todos} /> : <Text>Loading...</Text>}
|
|
808
|
-
<>);
|
|
809
|
-
```
|
|
810
|
-
|
|
811
|
-
The **metadata** is not reactive information and can only be modified from inside the global state lifecycle methods.
|
|
812
|
-
|
|
813
495
|
# Life cycle methods
|
|
814
496
|
|
|
815
497
|
There are some lifecycle methods available for use with global hooks, let's review them:
|
|
@@ -876,105 +558,17 @@ const useData = createGlobalState(
|
|
|
876
558
|
metadata: {
|
|
877
559
|
someExtraInformation: "someExtraInformation",
|
|
878
560
|
},
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
561
|
+
callbacks: {
|
|
562
|
+
// onSubscribed: (StateConfigCallbackParam) => {},
|
|
563
|
+
// onInit // etc
|
|
564
|
+
computePreventStateChange: ({ state, previousState }) => {
|
|
565
|
+
const prevent = isEqual(state, previousState);
|
|
883
566
|
|
|
884
|
-
|
|
567
|
+
return prevent;
|
|
568
|
+
},
|
|
885
569
|
},
|
|
886
570
|
}
|
|
887
571
|
);
|
|
888
572
|
```
|
|
889
573
|
|
|
890
|
-
Finally, if you have a very specific necessity but still want to use the global hooks, you can extend the **GlobalStoreAbstract** class. This will give you even more control over the state and the lifecycle of the global state.
|
|
891
|
-
|
|
892
|
-
Let's see an example again with the **asyncStorage** custom global hook but with the abstract class.
|
|
893
|
-
|
|
894
|
-
```ts
|
|
895
|
-
export class GlobalStore<
|
|
896
|
-
TState,
|
|
897
|
-
TMetadata extends {
|
|
898
|
-
asyncStorageKey?: string;
|
|
899
|
-
isAsyncStorageReady?: boolean;
|
|
900
|
-
} | null = null,
|
|
901
|
-
TStateSetter extends ActionCollectionConfig<TState, TMetadata> | StateSetter<TState> = StateSetter<TState>
|
|
902
|
-
> extends GlobalStoreAbstract<TState, TMetadata, TStateSetter> {
|
|
903
|
-
constructor(
|
|
904
|
-
state: TState,
|
|
905
|
-
config: GlobalStoreConfig<TState, TMetadata, TStateSetter> = {},
|
|
906
|
-
actionsConfig: TStateSetter | null = null
|
|
907
|
-
) {
|
|
908
|
-
super(state, config, actionsConfig);
|
|
909
|
-
|
|
910
|
-
this.initialize();
|
|
911
|
-
}
|
|
912
|
-
|
|
913
|
-
protected onInitialize = async ({
|
|
914
|
-
setState,
|
|
915
|
-
setMetadata,
|
|
916
|
-
getMetadata,
|
|
917
|
-
getState,
|
|
918
|
-
}: StateConfigCallbackParam<TState, TMetadata, TStateSetter>) => {
|
|
919
|
-
setMetadata({
|
|
920
|
-
...(metadata ?? {}),
|
|
921
|
-
isAsyncStorageReady: null,
|
|
922
|
-
});
|
|
923
|
-
|
|
924
|
-
const metadata = getMetadata();
|
|
925
|
-
const asyncStorageKey = metadata?.asyncStorageKey;
|
|
926
|
-
|
|
927
|
-
if (!asyncStorageKey) return;
|
|
928
|
-
|
|
929
|
-
const storedItem = (await asyncStorage.getItem(asyncStorageKey)) as string;
|
|
930
|
-
setMetadata({
|
|
931
|
-
...metadata,
|
|
932
|
-
isAsyncStorageReady: true,
|
|
933
|
-
});
|
|
934
|
-
|
|
935
|
-
if (storedItem === null) {
|
|
936
|
-
const state = getState();
|
|
937
|
-
|
|
938
|
-
// force the re-render of the subscribed components even if the state is the same
|
|
939
|
-
return setState(state, { forceUpdate: true });
|
|
940
|
-
}
|
|
941
|
-
|
|
942
|
-
const items = formatFromStore<TState>(storedItem, {
|
|
943
|
-
jsonParse: true,
|
|
944
|
-
});
|
|
945
|
-
|
|
946
|
-
setState(items, { forceUpdate: true });
|
|
947
|
-
};
|
|
948
|
-
|
|
949
|
-
protected onChange = ({ getMetadata, getState }: StoreTools<any, any> & StateChanges<unknown>) => {
|
|
950
|
-
const asyncStorageKey = getMetadata()?.asyncStorageKey;
|
|
951
|
-
|
|
952
|
-
if (!asyncStorageKey) return;
|
|
953
|
-
|
|
954
|
-
const state = getState();
|
|
955
|
-
|
|
956
|
-
const formattedObject = formatToStore(state, {
|
|
957
|
-
stringify: true,
|
|
958
|
-
});
|
|
959
|
-
|
|
960
|
-
asyncStorage.setItem(asyncStorageKey, formattedObject);
|
|
961
|
-
};
|
|
962
|
-
}
|
|
963
|
-
```
|
|
964
|
-
|
|
965
|
-
Then, from an instance of the global store, you will be able to access the hooks.
|
|
966
|
-
|
|
967
|
-
```ts
|
|
968
|
-
const storage = new GlobalStore(0, {
|
|
969
|
-
asyncStorage: {
|
|
970
|
-
key: "counter",
|
|
971
|
-
},
|
|
972
|
-
});
|
|
973
|
-
|
|
974
|
-
const [getState, _, getMetadata] = storage.getHookDecoupled();
|
|
975
|
-
const useState = storage.getHook();
|
|
976
|
-
```
|
|
977
|
-
|
|
978
|
-
### **Note**: The GlobalStore class is still available in the package in case you were already extending from it.
|
|
979
|
-
|
|
980
574
|
# That's it for now!! hope you enjoy coding!!
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-native-global-state-hooks",
|
|
3
|
-
"version": "8.0.
|
|
3
|
+
"version": "8.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": "./bundle.js",
|
|
6
6
|
"types": "./index.d.ts",
|
|
@@ -179,6 +179,6 @@
|
|
|
179
179
|
}
|
|
180
180
|
},
|
|
181
181
|
"dependencies": {
|
|
182
|
-
"react-hooks-global-states": "^6.0.
|
|
182
|
+
"react-hooks-global-states": "^6.0.7"
|
|
183
183
|
}
|
|
184
184
|
}
|