react-native-global-state-hooks 6.0.8 → 6.1.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
@@ -498,6 +498,122 @@ export const [useCount, getCount, $actions] =
498
498
 
499
499
  In the example the hook will work the same and you'll have access to the correct typing.
500
500
 
501
+ # Stateful Context with Actions
502
+
503
+ **The ultimate blend of flexibility and control in React state management!** You can now create an isolated global state within a React context, giving each consumer of the context provider a unique state instance. But that’s not all...
504
+
505
+ **Stateful Context with Actions** extends the powerful features of global hooks into the realm of React Context. By integrating global hooks within a context, you bring all the benefits of global state management—such as modularity, selectors, derived states, and actions—into a context-specific environment. This means each consumer of the context not only gets a unique state instance but also inherits all the advanced capabilities of global hooks.
506
+
507
+ ## Creating a Stateful Context
508
+
509
+ Forget about the boilerplate of creating a context... with **createStatefulContext** it's straightforward and powerful. You can create a context and provider with one line of code.
510
+
511
+ ```tsx
512
+ export const [useCounterContext, CounterProvider] = createStatefulContext(2);
513
+ ```
514
+
515
+ Then just wrap the components you need with the provider:
516
+
517
+ ```tsx
518
+ <CounterProvider>
519
+ <MyComponent />
520
+ </CounterProvider>
521
+ ```
522
+
523
+ And finally, access the context value with the generated custom hook:
524
+
525
+ ```tsx
526
+ const MyComponent = () => {
527
+ const [useCounter] = useCounterContext();
528
+
529
+ // If the component needs to react to state changes, simply use the hook
530
+ const [count, setCount] = useCounter();
531
+
532
+ return <>{count}</>;
533
+ };
534
+ ```
535
+
536
+ What’s the advantage of this, you might ask? Well, now you have all the capabilities of the global hooks within the isolated scope of the context. For example, you can choose whether or not to listen to changes in the state:
537
+
538
+ ```tsx
539
+ const MyComponent = () => {
540
+ const [, , setCount] = useCounterContext();
541
+
542
+ // This component can access only the setter of the state,
543
+ // and won't re-render if the counter changes
544
+ return (
545
+ <button onClick={() => setCount((count) => count + 1)}>Increase</button>
546
+ );
547
+ };
548
+ ```
549
+
550
+ Now you have selectors—if the state changes, the component will only re-render if the selected portion of the state changes.
551
+
552
+ ```tsx
553
+ const MyComponent = () => {
554
+ const [useCounter] = useCounterContext();
555
+
556
+ // Notice that we can select and derive values from the state
557
+ const [isEven, setCount] = useCounter((count) => count % 2 === 0);
558
+
559
+ useEffect(() => {
560
+ // Since the counter initially was 2 and now is 4, it’s still an even number.
561
+ // Because of this, the component will not re-render.
562
+ setCount(4);
563
+ }, []);
564
+
565
+ return <>{isEven ? "is even" : "is odd"}</>;
566
+ };
567
+ ```
568
+
569
+ **createStatefulContext** also allows you to add custom actions to control the manipulation of the state.
570
+
571
+ ```tsx
572
+ import { createStatefulContext, StoreTools } from "react-global-state-hooks";
573
+
574
+ type CounterState = {
575
+ count: number;
576
+ };
577
+
578
+ const initialState: CounterState = {
579
+ count: 0,
580
+ };
581
+
582
+ export const [useCounterContext, CounterProvider] = createStatefulContext(
583
+ initialState,
584
+ {
585
+ actions: {
586
+ increase: (value: number = 1) => {
587
+ return ({ setState }: StoreTools<CounterState>) => {
588
+ setState((state) => ({
589
+ ...state,
590
+ count: state.count + value,
591
+ }));
592
+ };
593
+ },
594
+ decrease: (value: number = 1) => {
595
+ return ({ setState }: StoreTools<CounterState>) => {
596
+ setState((state) => ({
597
+ ...state,
598
+ count: state.count - value,
599
+ }));
600
+ };
601
+ },
602
+ } as const,
603
+ }
604
+ );
605
+ ```
606
+
607
+ And just like with regular global hooks, now instead of a setter, the hook will return the collection of actions:
608
+
609
+ ```tsx
610
+ const MyComponent = () => {
611
+ const [, , actions] = useCounterContext();
612
+
613
+ return <button onClick={() => actions.increase(1)}>Increase</button>;
614
+ };
615
+ ```
616
+
501
617
  # Extending Global Hooks
502
618
 
503
619
  ## `[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.
@@ -0,0 +1,6 @@
1
+ import { TMetadataResult } from "GlobalStore.types";
2
+ import React from "react";
3
+ import { ActionCollectionConfig, createStateConfig, StateHook, StateSetter, ActionCollectionResult, StateGetter } from "react-hooks-global-states";
4
+ export declare const createStatefulContext: <TState, TMetadata = null, TActions extends ActionCollectionConfig<TState, TMetadata> = null>(initialValue: TState, parameters?: createStateConfig<TState, TMetadata, TActions>) => readonly [() => [hook: StateHook<TState, keyof TActions extends never ? StateSetter<TState> : ActionCollectionResult<TState, TMetadataResult<TMetadata>, TActions>, TMetadataResult<TMetadata>>, getter: StateGetter<TState>, setter: keyof TActions extends never ? StateSetter<TState> : ActionCollectionResult<TState, TMetadata, TActions>], React.FC<React.PropsWithChildren<{
5
+ initialValue?: Partial<TState>;
6
+ }>>];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-global-state-hooks",
3
- "version": "6.0.8",
3
+ "version": "6.1.0",
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",