|
3301 | 3301 | * @since v3.0.0 |
3302 | 3302 | */ |
3303 | 3303 | /// useValueListener |
| 3304 | +/** |
| 3305 | + * The useStartTransactionListener hook registers a listener function with the |
| 3306 | + * Store that will be called at the start of a transaction. |
| 3307 | + * |
| 3308 | + * Unlike the addStartTransactionListener method, which returns a listener Id |
| 3309 | + * and requires you to remove it manually, the useStartTransactionListener hook |
| 3310 | + * manages this lifecycle for you: when the listener changes (per its |
| 3311 | + * `listenerDeps` dependencies) or the component unmounts, the listener on the |
| 3312 | + * underlying Store will be deleted. |
| 3313 | + * @param listener The function that will be called at the start of a |
| 3314 | + * transaction. |
| 3315 | + * @param listenerDeps An optional array of dependencies for the `listener` |
| 3316 | + * function, which, if any change, result in the re-registration of the |
| 3317 | + * listener. This parameter defaults to an empty array. |
| 3318 | + * @param storeOrStoreId The Store to register the listener with: omit for the |
| 3319 | + * default context Store, provide an Id for a named context Store, or provide an |
| 3320 | + * explicit reference. |
| 3321 | + * @example |
| 3322 | + * This example uses the useStartTransactionListener hook to create a listener |
| 3323 | + * that is scoped to a single component. When the component is unmounted, the |
| 3324 | + * listener is removed from the Store. |
| 3325 | + * |
| 3326 | + * ```jsx |
| 3327 | + * const App = ({store}) => ( |
| 3328 | + * <Provider store={store}> |
| 3329 | + * <Pane /> |
| 3330 | + * </Provider> |
| 3331 | + * ); |
| 3332 | + * const Pane = () => { |
| 3333 | + * useStartTransactionListener(() => console.log('Start transaction')); |
| 3334 | + * return <span>App</span>; |
| 3335 | + * }; |
| 3336 | + * |
| 3337 | + * const store = createStore(); |
| 3338 | + * const app = document.createElement('div'); |
| 3339 | + * const root = ReactDOMClient.createRoot(app); |
| 3340 | + * root.render(<App store={store} />); // !act |
| 3341 | + * console.log(store.getListenerStats().transaction); |
| 3342 | + * // -> 1 |
| 3343 | + * |
| 3344 | + * store.setValue('open', false); // !act |
| 3345 | + * // -> 'Start transaction' |
| 3346 | + * |
| 3347 | + * root.unmount(); // !act |
| 3348 | + * console.log(store.getListenerStats().transaction); |
| 3349 | + * // -> 0 |
| 3350 | + * ``` |
| 3351 | + * @category Store hooks |
| 3352 | + * @since v4.2.2 |
| 3353 | + */ |
| 3354 | +/// useStartTransactionListener |
| 3355 | +/** |
| 3356 | + * The useWillFinishTransactionListener hook registers a listener function with |
| 3357 | + * a Store that will be called just before other non-mutating listeners are |
| 3358 | + * called at the end of the transaction. |
| 3359 | + * |
| 3360 | + * Unlike the addWillFinisTransactionListener method, which returns a listener |
| 3361 | + * Id and requires you to remove it manually, the |
| 3362 | + * useWillFinishTransactionListener hook manages this lifecycle for you: when |
| 3363 | + * the listener changes (per its `listenerDeps` dependencies) or the component |
| 3364 | + * unmounts, the listener on the underlying Store will be deleted. |
| 3365 | + * @param listener The function that will be called before the end of a |
| 3366 | + * transaction. |
| 3367 | + * @param listenerDeps An optional array of dependencies for the `listener` |
| 3368 | + * function, which, if any change, result in the re-registration of the |
| 3369 | + * listener. This parameter defaults to an empty array. |
| 3370 | + * @param storeOrStoreId The Store to register the listener with: omit for the |
| 3371 | + * default context Store, provide an Id for a named context Store, or provide an |
| 3372 | + * explicit reference. |
| 3373 | + * @example |
| 3374 | + * This example uses the useWillFinishTransactionListener hook to create a |
| 3375 | + * listener that is scoped to a single component. When the component is |
| 3376 | + * unmounted, the listener is removed from the Store. |
| 3377 | + * |
| 3378 | + * ```jsx |
| 3379 | + * const App = ({store}) => ( |
| 3380 | + * <Provider store={store}> |
| 3381 | + * <Pane /> |
| 3382 | + * </Provider> |
| 3383 | + * ); |
| 3384 | + * const Pane = () => { |
| 3385 | + * useWillFinishTransactionListener( |
| 3386 | + * () => console.log('Will finish transaction'), |
| 3387 | + * ); |
| 3388 | + * return <span>App</span>; |
| 3389 | + * }; |
| 3390 | + * |
| 3391 | + * const store = createStore(); |
| 3392 | + * const app = document.createElement('div'); |
| 3393 | + * const root = ReactDOMClient.createRoot(app); |
| 3394 | + * root.render(<App store={store} />); // !act |
| 3395 | + * console.log(store.getListenerStats().transaction); |
| 3396 | + * // -> 1 |
| 3397 | + * |
| 3398 | + * store.setValue('open', false); // !act |
| 3399 | + * // -> 'Will finish transaction' |
| 3400 | + * |
| 3401 | + * root.unmount(); // !act |
| 3402 | + * console.log(store.getListenerStats().transaction); |
| 3403 | + * // -> 0 |
| 3404 | + * ``` |
| 3405 | + * @category Store hooks |
| 3406 | + * @since v4.2.2 |
| 3407 | + */ |
| 3408 | +/// useWillFinishTransactionListener |
| 3409 | +/** |
| 3410 | + * The useDidFinishTransactionListener hook registers a listener function with a |
| 3411 | + * Store that will be called just after other non-mutating listeners are called |
| 3412 | + * at the end of the transaction. |
| 3413 | + * |
| 3414 | + * Unlike the addDidFinishTransactionListener method, which returns a listener |
| 3415 | + * Id and requires you to remove it manually, the |
| 3416 | + * useDidFinishTransactionListener hook manages this lifecycle for you: when the |
| 3417 | + * listener changes (per its `listenerDeps` dependencies) or the component |
| 3418 | + * unmounts, the listener on the underlying Store will be deleted. |
| 3419 | + * @param listener The function that will be called after the end of a |
| 3420 | + * transaction. |
| 3421 | + * @param listenerDeps An optional array of dependencies for the `listener` |
| 3422 | + * function, which, if any change, result in the re-registration of the |
| 3423 | + * listener. This parameter defaults to an empty array. |
| 3424 | + * @param storeOrStoreId The Store to register the listener with: omit for the |
| 3425 | + * default context Store, provide an Id for a named context Store, or provide an |
| 3426 | + * explicit reference. |
| 3427 | + * @example |
| 3428 | + * This example uses the useDidFinishTransactionListener hook to create a |
| 3429 | + * listener that is scoped to a single component. When the component is |
| 3430 | + * unmounted, the listener is removed from the Store. |
| 3431 | + * |
| 3432 | + * ```jsx |
| 3433 | + * const App = ({store}) => ( |
| 3434 | + * <Provider store={store}> |
| 3435 | + * <Pane /> |
| 3436 | + * </Provider> |
| 3437 | + * ); |
| 3438 | + * const Pane = () => { |
| 3439 | + * useDidFinishTransactionListener( |
| 3440 | + * () => console.log('Did finish transaction'), |
| 3441 | + * ); |
| 3442 | + * return <span>App</span>; |
| 3443 | + * }; |
| 3444 | + * |
| 3445 | + * const store = createStore(); |
| 3446 | + * const app = document.createElement('div'); |
| 3447 | + * const root = ReactDOMClient.createRoot(app); |
| 3448 | + * root.render(<App store={store} />); // !act |
| 3449 | + * console.log(store.getListenerStats().transaction); |
| 3450 | + * // -> 1 |
| 3451 | + * |
| 3452 | + * store.setValue('open', false); // !act |
| 3453 | + * // -> 'Did finish transaction' |
| 3454 | + * |
| 3455 | + * root.unmount(); // !act |
| 3456 | + * console.log(store.getListenerStats().transaction); |
| 3457 | + * // -> 0 |
| 3458 | + * ``` |
| 3459 | + * @category Store hooks |
| 3460 | + * @since v4.2.2 |
| 3461 | + */ |
| 3462 | +/// useDidFinishTransactionListener |
3304 | 3463 | /** |
3305 | 3464 | * The useCreateMetrics hook is used to create a Metrics object within a React |
3306 | 3465 | * application with convenient memoization. |
|
0 commit comments