@ptolemy2002/react-proxy-context 2.0.0 → 2.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 +67 -22
- package/dist/main.d.ts +5 -4
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -2,54 +2,99 @@
|
|
|
2
2
|
This library is a solution for React that uses Javascript Proxies to allow React context consumers to listen to mutations as well as reassignments. Another benefit is that you can listen to the mutation of only specific properties. I find that many consider this style more intuitive to work with, though it is considered bad practice by others. A limitation is that it is only able to track direct mutations, not mutations that occur in nested arrays or objects, so those will still require recreation and reassignment.
|
|
3
3
|
|
|
4
4
|
The functions are not exported as default, so you can import them in one of the following ways:
|
|
5
|
-
```
|
|
5
|
+
```javascript
|
|
6
6
|
// ES6
|
|
7
7
|
import { functionName } from '@ptolemy2002/react-proxy-context';
|
|
8
8
|
// CommonJS
|
|
9
9
|
const { functionName } = require('@ptolemy2002/react-proxy-context');
|
|
10
10
|
```
|
|
11
11
|
|
|
12
|
+
## Type Reference
|
|
13
|
+
```typescript
|
|
14
|
+
type ContextWithName<T> = React.Context<T> & { name: string };
|
|
15
|
+
|
|
16
|
+
type OnChangePropCallback<T> =
|
|
17
|
+
<K extends keyof T>(prop: K, current: T[K], prev?: T[K]) => void
|
|
18
|
+
;
|
|
19
|
+
type OnChangeReinitCallback<T> =
|
|
20
|
+
(current: T, prev?: T) => void
|
|
21
|
+
;
|
|
22
|
+
|
|
23
|
+
type Dependency<T> = keyof T | (
|
|
24
|
+
<K extends keyof T>(prop: K, current: T[K], prev: T[K], obj: T) => boolean
|
|
25
|
+
) | null | undefined | false;
|
|
26
|
+
|
|
27
|
+
type ProxyContext<T> = ContextWithName<ProxyContextValue<T> | undefined>;
|
|
28
|
+
|
|
29
|
+
type ProxyContextValue<T> = {
|
|
30
|
+
obj: T;
|
|
31
|
+
set: (newObj: T) => T;
|
|
32
|
+
subscribe: (
|
|
33
|
+
propCallback: OnChangePropCallback<T>,
|
|
34
|
+
reinitCallback: OnChangeReinitCallback<T>,
|
|
35
|
+
deps: Dependency<T>[]
|
|
36
|
+
) => string;
|
|
37
|
+
unsubscribe: (id: string) => void;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
type ProxyContextProviderProps<T> = {
|
|
41
|
+
children: ReactNode;
|
|
42
|
+
value: T;
|
|
43
|
+
onChangeProp?: OnChangePropCallback<T>;
|
|
44
|
+
onChangeReinit?: OnChangeReinitCallback<T>;
|
|
45
|
+
proxyRef?: React.MutableRefObject<T>;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
type UseProxyContextResult<T> = HookResultData<{
|
|
49
|
+
value: T;
|
|
50
|
+
set: (newObj: T) => T;
|
|
51
|
+
}, readonly [T, (newObj: T) => T]>;
|
|
52
|
+
```
|
|
53
|
+
|
|
12
54
|
## Functions
|
|
13
55
|
The following functions are available in the library:
|
|
14
56
|
|
|
15
|
-
### createProxyContext
|
|
57
|
+
### createProxyContext<T>
|
|
16
58
|
#### Description
|
|
17
|
-
Creates a new instance of the ProxyContext, essentially to be used as the context type, with the specified
|
|
59
|
+
Creates a new instance of the ProxyContext, essentially to be used as the context type, with the specified name. This is effectively just the normal React createContext function, but with a check to ensure that the browser supports Proxies. `T` represents the type of the object that will be stored in the context.
|
|
18
60
|
|
|
19
61
|
#### Parameters
|
|
20
|
-
- `defaultValue` (Object): The default value of the context. This is what is reported when the context is not provided.
|
|
21
62
|
- `name` (String): The name of the context. This is used for debugging purposes.
|
|
22
63
|
|
|
23
64
|
#### Returns
|
|
24
|
-
|
|
65
|
+
`ProxyContext<T>` - The context object that can be used in a provider.
|
|
25
66
|
|
|
26
|
-
|
|
27
|
-
The following components are available in the library:
|
|
28
|
-
|
|
29
|
-
### ProxyContextProvider
|
|
67
|
+
### createProxyContextProvider<T extends object>
|
|
30
68
|
#### Description
|
|
31
|
-
|
|
69
|
+
Creates a new proxy context provider component with the specified type. `ProxyContextProvider` is no longer used due to a TypeScript limitation that prevents the context type from being inferred.
|
|
70
|
+
|
|
71
|
+
#### Parameters
|
|
72
|
+
- `contextClass` (`ProxyContext<T>`): The context class that was created using `createProxyContext`.
|
|
73
|
+
|
|
74
|
+
#### Returns
|
|
75
|
+
`React.MemoExoticComponent<FunctionComponent<ProxyContextProviderProps<T> & { renderDeps?: any[] }>>` - The provider component that can be used in the React tree. The resulting component is memoized to prevent unnecessary re-renders, but the `renderDeps` prop can be used to force a re-render when the specified dependencies change (necessary when working with the children prop).
|
|
32
76
|
|
|
33
|
-
|
|
34
|
-
- `
|
|
35
|
-
- `
|
|
36
|
-
- `
|
|
37
|
-
- `proxyRef` (
|
|
77
|
+
The component has the following other props:
|
|
78
|
+
- `value` (T): The value of the context. This is what is reported when the context is not provided.
|
|
79
|
+
- `onChangeProp` (`OnChangePropCallback<T>`): A function that is called whenever a property of the context is changed. The first parameter is the property that was changed, the second parameter is the current value of the property, and the third parameter is the previous value of the property. This is useful for listening to changes in the provider's parent component.
|
|
80
|
+
- `onChangeReinit` (`OnChangeReinitCallback<T>`): A function that is called whenever the context is reinitialized. The first parameter is the current value of the context, and the second parameter is the previous value of the context. This is useful for listening to changes in the provider's parent component.
|
|
81
|
+
- `proxyRef` (`React.MutableRefObject<T>`): A ref object that is assigned the proxy object of the context. This is useful for accessing the proxy object directly by the provider's parent component.
|
|
38
82
|
|
|
39
83
|
## Hooks
|
|
40
84
|
The following hooks are available in the library:
|
|
41
85
|
|
|
42
|
-
### useProxyContext
|
|
43
|
-
A hook that uses the context provided by the `ProxyContextProvider` component. This hook also provides options to choose which properties to listen to and whether to listen to full reassignments.
|
|
86
|
+
### useProxyContext<T>
|
|
87
|
+
A hook that uses the context provided by the `ProxyContextProvider` component. This hook also provides options to choose which properties to listen to and whether to listen to full reassignments. `T` represents the type of the object that is stored in the context.
|
|
44
88
|
|
|
45
89
|
#### Parameters
|
|
46
|
-
- `contextClass` (
|
|
47
|
-
- `deps` (
|
|
48
|
-
- `
|
|
49
|
-
- `
|
|
90
|
+
- `contextClass` (`ProxyContext<T>`): The context class that was created using `createProxyContext`.
|
|
91
|
+
- `deps` (`Dependency<T>[] | null`): An array of dependencies to listen to. If any of these properties on the context change, the hook will re-render. If this is falsy, any mutation will trigger a re-render. You can also specify a function that returns a boolean to determine whether to re-render. If this is null, the hook will re-render on any mutation. By default, this is an empty array.
|
|
92
|
+
- `onChangeProp` (`OnChangePropCallback<T> | undefined`): A function that is called whenever a property of the context is changed. The first parameter is the property that was changed, the second parameter is the current value of the property, and the third parameter is the previous value of the property. This is useful for listening to changes in the provider's parent component.
|
|
93
|
+
- `onChangeReinit` (`OnChangeReinitCallback<T> | undefined`): A function that is called whenever the context is reinitialized. The first parameter is the current value of the context, and the second parameter is the previous value of the context. This is useful for listening to changes in the provider's parent component.
|
|
94
|
+
- `listenReinit` (`boolean`): Whether to listen to full reassignments of the context. If this is true, the hook will re-render whenever the context is reinitialized. By default, this is true.
|
|
50
95
|
|
|
51
96
|
#### Returns
|
|
52
|
-
|
|
97
|
+
`UseProxyContextResult<T>` - An object containing the current value of the context and a function to set the context. The function returns the new value of the context wrapped in a `Proxy`.
|
|
53
98
|
|
|
54
99
|
## Peer Dependencies
|
|
55
100
|
These should be installed in order to use the library, as npm does not automatically add peer dependencies to your project.
|
package/dist/main.d.ts
CHANGED
|
@@ -8,11 +8,12 @@ export type OnChangeReinitCallback<T> = (current: T, prev?: T) => void;
|
|
|
8
8
|
export type ProxyContextValue<T> = {
|
|
9
9
|
obj: T;
|
|
10
10
|
set: (newObj: T) => T;
|
|
11
|
-
subscribe: (propCallback: OnChangePropCallback<T>, reinitCallback: OnChangeReinitCallback<T>, deps: Dependency<T>[]) => string;
|
|
11
|
+
subscribe: (propCallback: OnChangePropCallback<T>, reinitCallback: OnChangeReinitCallback<T>, deps: Dependency<T>[] | null) => string;
|
|
12
12
|
unsubscribe: (id: string) => void;
|
|
13
13
|
};
|
|
14
|
-
export declare function createProxyContext<T>(name: string):
|
|
14
|
+
export declare function createProxyContext<T>(name: string): ProxyContext<T>;
|
|
15
15
|
export type Dependency<T> = keyof T | (<K extends keyof T>(prop: K, current: T[K], prev: T[K], obj: T) => boolean) | null | undefined | false;
|
|
16
|
+
export type ProxyContext<T> = ContextWithName<ProxyContextValue<T> | undefined>;
|
|
16
17
|
export type ProxyContextProviderProps<T> = {
|
|
17
18
|
children: ReactNode;
|
|
18
19
|
value: T;
|
|
@@ -20,11 +21,11 @@ export type ProxyContextProviderProps<T> = {
|
|
|
20
21
|
onChangeReinit?: OnChangeReinitCallback<T>;
|
|
21
22
|
proxyRef?: React.MutableRefObject<T>;
|
|
22
23
|
};
|
|
23
|
-
export declare function createProxyContextProvider<T extends object>(contextClass:
|
|
24
|
+
export declare function createProxyContextProvider<T extends object>(contextClass: ProxyContext<T>): import('react').MemoExoticComponent<import('react').FunctionComponent<ProxyContextProviderProps<T> & {
|
|
24
25
|
renderDeps?: any[];
|
|
25
26
|
}>>;
|
|
26
27
|
export type UseProxyContextResult<T> = HookResultData<{
|
|
27
28
|
value: T;
|
|
28
29
|
set: (newObj: T) => T;
|
|
29
30
|
}, readonly [T, (newObj: T) => T]>;
|
|
30
|
-
export declare function useProxyContext<T>(contextClass:
|
|
31
|
+
export declare function useProxyContext<T>(contextClass: ProxyContext<T>, deps?: Dependency<T>[] | null, onChangeProp?: OnChangePropCallback<T>, onChangeReinit?: OnChangeReinitCallback<T>, listenReinit?: boolean): UseProxyContextResult<T>;
|