@faasjs/react 3.5.2 → 3.6.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
@@ -22,6 +22,7 @@ React plugin for FaasJS.
22
22
  - [useEqualCallback](functions/useEqualCallback.md): Memoize a callback with deep equality.
23
23
  - [useConstant](functions/useConstant.md): Create a constant value with hooks.
24
24
  - [createSplittingContext](functions/createSplittingContext.md): Create a context for code splitting.
25
+ - [splittingState](functions/splittingState.md): Create a splitting states.
25
26
  - [OptionalWrapper](functions/OptionalWrapper.md): Render a component optionally.
26
27
  - [ErrorBoundary](classes/ErrorBoundary.md): Catch errors in the component tree.
27
28
  - Fetch Data:
@@ -51,6 +52,7 @@ npm install @faasjs/react
51
52
  - [useEqualMemo](functions/useEqualMemo.md)
52
53
  - [useEqualMemoize](functions/useEqualMemoize.md)
53
54
  - [useFaas](functions/useFaas.md)
55
+ - [useSplittingState](functions/useSplittingState.md)
54
56
  - [withFaasData](functions/withFaasData.md)
55
57
 
56
58
  ## Classes
package/dist/index.d.mts CHANGED
@@ -2,7 +2,7 @@ import { FaasAction, FaasData, FaasParams } from '@faasjs/types';
2
2
  export { FaasAction, FaasData, FaasParams } from '@faasjs/types';
3
3
  import { Response, BaseUrl, ResponseError, Options, FaasBrowserClient } from '@faasjs/browser';
4
4
  export { Options, Response, ResponseError, ResponseHeaders } from '@faasjs/browser';
5
- import { ReactNode, ReactElement, Component, ComponentType, ComponentProps } from 'react';
5
+ import { ReactNode, Dispatch, SetStateAction, ReactElement, Component, ComponentType, ComponentProps } from 'react';
6
6
  import * as react_jsx_runtime from 'react/jsx-runtime';
7
7
 
8
8
  /**
@@ -105,21 +105,75 @@ declare function createSplittingContext<T extends Record<string, any>>(defaultVa
105
105
  } | (keyof T)[]): {
106
106
  /**
107
107
  * The provider component of the splitting context.
108
+ *
108
109
  * @see https://faasjs.com/doc/react/functions/createSplittingContext.html#provider
110
+ *
111
+ * @example
112
+ * ```tsx
113
+ * function App() {
114
+ * const [value, setValue] = useState(0)
115
+ *
116
+ * return (
117
+ * <Provider value={{ value, setValue }}>
118
+ * <ReaderComponent />
119
+ * <WriterComponent />
120
+ * </Provider>
121
+ * )
122
+ * }
123
+ * ```
109
124
  */
110
125
  Provider<NewT extends T = T>(props: {
111
126
  value?: NewT;
112
127
  children: ReactNode;
128
+ /**
129
+ * Whether to use memoization for the children.
130
+ *
131
+ * @default false
132
+ *
133
+ * `true`: memoize the children without dependencies.
134
+ * `any[]`: memoize the children with specific dependencies.
135
+ */
113
136
  memo?: true | any[];
114
137
  }): ReactNode;
115
138
  /**
116
139
  * The hook to use the splitting context.
117
140
  *
118
141
  * @see https://faasjs.com/doc/react/functions/createSplittingContext.html#use
142
+ *
143
+ * @example
144
+ * ```tsx
145
+ * function ChildComponent() {
146
+ * const { value, setValue } = use()
147
+ *
148
+ * return <div>{value}<button onClick={() => setValue(1)}>change value</button></div>
149
+ * }
150
+ * ```
119
151
  */
120
152
  use: <NewT extends T = T>() => Readonly<NewT>;
121
153
  };
122
154
 
155
+ type SetPrefix<S extends string | number | symbol> = S extends string ? (S extends `${infer First}${infer Rest}` ? `set${Capitalize<First>}${Rest}` : never) : never;
156
+ type StateSetters<T> = {
157
+ [K in keyof T as SetPrefix<K>]: Dispatch<SetStateAction<T[K]>>;
158
+ };
159
+ type StatesWithSetters<T> = T & StateSetters<T>;
160
+ /**
161
+ * A hook that initializes and splits state variables and their corresponding setters.
162
+ *
163
+ * @template T - A generic type that extends a record with string keys and any values.
164
+ * @param {T} initialStates - An object containing the initial states.
165
+ *
166
+ * @example
167
+ * ```tsx
168
+ * function Counter() {
169
+ * const { count, setCount, name, setName } = useSplittingState({ count: 0, name: 'John' });
170
+ *
171
+ * return <>{name}: {count}</>
172
+ * }
173
+ * ```
174
+ */
175
+ declare function useSplittingState<T extends Record<string, unknown>>(initialStates: T): StatesWithSetters<T>;
176
+
123
177
  /**
124
178
  * Injects FaasData props.
125
179
  */
@@ -317,4 +371,4 @@ declare const OptionalWrapper: React.FC<OptionalWrapperProps> & {
317
371
  whyDidYouRender: boolean;
318
372
  };
319
373
 
320
- export { ErrorBoundary, type ErrorBoundaryProps, type ErrorChildrenProps, type FaasDataInjection, FaasDataWrapper, type FaasDataWrapperProps, FaasReactClient, type FaasReactClientInstance, type FaasReactClientOptions, type OnError, OptionalWrapper, type OptionalWrapperProps, createSplittingContext, equal, faas, getClient, useConstant, useEqualCallback, useEqualEffect, useEqualMemo, useEqualMemoize, useFaas, type useFaasOptions, withFaasData };
374
+ export { ErrorBoundary, type ErrorBoundaryProps, type ErrorChildrenProps, type FaasDataInjection, FaasDataWrapper, type FaasDataWrapperProps, FaasReactClient, type FaasReactClientInstance, type FaasReactClientOptions, type OnError, OptionalWrapper, type OptionalWrapperProps, createSplittingContext, equal, faas, getClient, useConstant, useEqualCallback, useEqualEffect, useEqualMemo, useEqualMemoize, useFaas, type useFaasOptions, useSplittingState, withFaasData };
package/dist/index.d.ts CHANGED
@@ -2,7 +2,7 @@ import { FaasAction, FaasData, FaasParams } from '@faasjs/types';
2
2
  export { FaasAction, FaasData, FaasParams } from '@faasjs/types';
3
3
  import { Response, BaseUrl, ResponseError, Options, FaasBrowserClient } from '@faasjs/browser';
4
4
  export { Options, Response, ResponseError, ResponseHeaders } from '@faasjs/browser';
5
- import { ReactNode, ReactElement, Component, ComponentType, ComponentProps } from 'react';
5
+ import { ReactNode, Dispatch, SetStateAction, ReactElement, Component, ComponentType, ComponentProps } from 'react';
6
6
  import * as react_jsx_runtime from 'react/jsx-runtime';
7
7
 
8
8
  /**
@@ -105,21 +105,75 @@ declare function createSplittingContext<T extends Record<string, any>>(defaultVa
105
105
  } | (keyof T)[]): {
106
106
  /**
107
107
  * The provider component of the splitting context.
108
+ *
108
109
  * @see https://faasjs.com/doc/react/functions/createSplittingContext.html#provider
110
+ *
111
+ * @example
112
+ * ```tsx
113
+ * function App() {
114
+ * const [value, setValue] = useState(0)
115
+ *
116
+ * return (
117
+ * <Provider value={{ value, setValue }}>
118
+ * <ReaderComponent />
119
+ * <WriterComponent />
120
+ * </Provider>
121
+ * )
122
+ * }
123
+ * ```
109
124
  */
110
125
  Provider<NewT extends T = T>(props: {
111
126
  value?: NewT;
112
127
  children: ReactNode;
128
+ /**
129
+ * Whether to use memoization for the children.
130
+ *
131
+ * @default false
132
+ *
133
+ * `true`: memoize the children without dependencies.
134
+ * `any[]`: memoize the children with specific dependencies.
135
+ */
113
136
  memo?: true | any[];
114
137
  }): ReactNode;
115
138
  /**
116
139
  * The hook to use the splitting context.
117
140
  *
118
141
  * @see https://faasjs.com/doc/react/functions/createSplittingContext.html#use
142
+ *
143
+ * @example
144
+ * ```tsx
145
+ * function ChildComponent() {
146
+ * const { value, setValue } = use()
147
+ *
148
+ * return <div>{value}<button onClick={() => setValue(1)}>change value</button></div>
149
+ * }
150
+ * ```
119
151
  */
120
152
  use: <NewT extends T = T>() => Readonly<NewT>;
121
153
  };
122
154
 
155
+ type SetPrefix<S extends string | number | symbol> = S extends string ? (S extends `${infer First}${infer Rest}` ? `set${Capitalize<First>}${Rest}` : never) : never;
156
+ type StateSetters<T> = {
157
+ [K in keyof T as SetPrefix<K>]: Dispatch<SetStateAction<T[K]>>;
158
+ };
159
+ type StatesWithSetters<T> = T & StateSetters<T>;
160
+ /**
161
+ * A hook that initializes and splits state variables and their corresponding setters.
162
+ *
163
+ * @template T - A generic type that extends a record with string keys and any values.
164
+ * @param {T} initialStates - An object containing the initial states.
165
+ *
166
+ * @example
167
+ * ```tsx
168
+ * function Counter() {
169
+ * const { count, setCount, name, setName } = useSplittingState({ count: 0, name: 'John' });
170
+ *
171
+ * return <>{name}: {count}</>
172
+ * }
173
+ * ```
174
+ */
175
+ declare function useSplittingState<T extends Record<string, unknown>>(initialStates: T): StatesWithSetters<T>;
176
+
123
177
  /**
124
178
  * Injects FaasData props.
125
179
  */
@@ -317,4 +371,4 @@ declare const OptionalWrapper: React.FC<OptionalWrapperProps> & {
317
371
  whyDidYouRender: boolean;
318
372
  };
319
373
 
320
- export { ErrorBoundary, type ErrorBoundaryProps, type ErrorChildrenProps, type FaasDataInjection, FaasDataWrapper, type FaasDataWrapperProps, FaasReactClient, type FaasReactClientInstance, type FaasReactClientOptions, type OnError, OptionalWrapper, type OptionalWrapperProps, createSplittingContext, equal, faas, getClient, useConstant, useEqualCallback, useEqualEffect, useEqualMemo, useEqualMemoize, useFaas, type useFaasOptions, withFaasData };
374
+ export { ErrorBoundary, type ErrorBoundaryProps, type ErrorChildrenProps, type FaasDataInjection, FaasDataWrapper, type FaasDataWrapperProps, FaasReactClient, type FaasReactClientInstance, type FaasReactClientOptions, type OnError, OptionalWrapper, type OptionalWrapperProps, createSplittingContext, equal, faas, getClient, useConstant, useEqualCallback, useEqualEffect, useEqualMemo, useEqualMemoize, useFaas, type useFaasOptions, useSplittingState, withFaasData };
package/dist/index.js CHANGED
@@ -115,6 +115,14 @@ function createSplittingContext(defaultValue) {
115
115
  use
116
116
  };
117
117
  }
118
+ function useSplittingState(initialStates) {
119
+ const states = {};
120
+ for (const key of Object.keys(initialStates)) {
121
+ const state = react.useState(initialStates[key]);
122
+ Object.assign(states, { [key]: state[0], [`set${String(key).charAt(0).toUpperCase()}${String(key).slice(1)}`]: state[1] });
123
+ }
124
+ return states;
125
+ }
118
126
  function FaasDataWrapper(props) {
119
127
  const request = getClient(props.baseUrl).useFaas(
120
128
  props.action,
@@ -340,4 +348,5 @@ exports.useEqualEffect = useEqualEffect;
340
348
  exports.useEqualMemo = useEqualMemo;
341
349
  exports.useEqualMemoize = useEqualMemoize;
342
350
  exports.useFaas = useFaas;
351
+ exports.useSplittingState = useSplittingState;
343
352
  exports.withFaasData = withFaasData;
package/dist/index.mjs CHANGED
@@ -113,6 +113,14 @@ function createSplittingContext(defaultValue) {
113
113
  use
114
114
  };
115
115
  }
116
+ function useSplittingState(initialStates) {
117
+ const states = {};
118
+ for (const key of Object.keys(initialStates)) {
119
+ const state = useState(initialStates[key]);
120
+ Object.assign(states, { [key]: state[0], [`set${String(key).charAt(0).toUpperCase()}${String(key).slice(1)}`]: state[1] });
121
+ }
122
+ return states;
123
+ }
116
124
  function FaasDataWrapper(props) {
117
125
  const request = getClient(props.baseUrl).useFaas(
118
126
  props.action,
@@ -324,4 +332,4 @@ var OptionalWrapper = ({ condition, Wrapper, wrapperProps, children }) => {
324
332
  };
325
333
  OptionalWrapper.whyDidYouRender = true;
326
334
 
327
- export { ErrorBoundary, FaasDataWrapper, FaasReactClient, OptionalWrapper, createSplittingContext, equal, faas, getClient, useConstant, useEqualCallback, useEqualEffect, useEqualMemo, useEqualMemoize, useFaas, withFaasData };
335
+ export { ErrorBoundary, FaasDataWrapper, FaasReactClient, OptionalWrapper, createSplittingContext, equal, faas, getClient, useConstant, useEqualCallback, useEqualEffect, useEqualMemo, useEqualMemoize, useFaas, useSplittingState, withFaasData };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@faasjs/react",
3
- "version": "3.5.2",
3
+ "version": "3.6.1",
4
4
  "license": "MIT",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -35,12 +35,12 @@
35
35
  ],
36
36
  "peerDependencies": {
37
37
  "react": "*",
38
- "@faasjs/browser": "3.5.2"
38
+ "@faasjs/browser": "3.6.1"
39
39
  },
40
40
  "devDependencies": {
41
41
  "@types/react": "*",
42
42
  "react": "*",
43
- "@faasjs/browser": "3.5.2"
43
+ "@faasjs/browser": "3.6.1"
44
44
  },
45
45
  "engines": {
46
46
  "node": ">=22.0.0",