|
2563 | 2563 | * @category Store hooks |
2564 | 2564 | */ |
2565 | 2565 | /// useTableListener |
| 2566 | +/** |
| 2567 | + * The useTableCellIdsListener hook registers a listener function with a Store |
| 2568 | + * that will be called whenever the Cell Ids that appear anywhere in a Table |
| 2569 | + * change. |
| 2570 | + * |
| 2571 | + * This hook is useful for situations where a component needs to register its |
| 2572 | + * own specific listener to do more than simply tracking the value (which is |
| 2573 | + * more easily done with the useTableCellIds hook). |
| 2574 | + * |
| 2575 | + * You can either listen to a single Table (by specifying its Id as the method's |
| 2576 | + * first parameter) or changes to any Table (by providing `null`). |
| 2577 | + * |
| 2578 | + * Unlike the addTableCellIdsListener method, which returns a listener Id and |
| 2579 | + * requires you to remove it manually, the useTableCellIdsListener hook manages |
| 2580 | + * this lifecycle for you: when the listener changes (per its `listenerDeps` |
| 2581 | + * dependencies) or the component unmounts, the listener on the underlying Store |
| 2582 | + * will be deleted. |
| 2583 | + * |
| 2584 | + * @param tableId The Id of the Table to listen to, or `null` as a wildcard. |
| 2585 | + * @param listener The function that will be called whenever the Cell Ids that |
| 2586 | + * appear anywhere in a Table change. |
| 2587 | + * @param listenerDeps An optional array of dependencies for the `listener` |
| 2588 | + * function, which, if any change, result in the re-registration of the |
| 2589 | + * listener. This parameter defaults to an empty array. |
| 2590 | + * @param mutator An optional boolean that indicates that the listener mutates |
| 2591 | + * Store data. |
| 2592 | + * @param storeOrStoreId The Store to register the listener with: omit for the |
| 2593 | + * default context Store, provide an Id for a named context Store, or provide an |
| 2594 | + * explicit reference. |
| 2595 | + * @example |
| 2596 | + * This example uses the useTableCellIdsListener hook to create a listener that |
| 2597 | + * is scoped to a single component. When the component is unmounted, the |
| 2598 | + * listener is removed from the Store. |
| 2599 | + * |
| 2600 | + * ```jsx |
| 2601 | + * const App = ({store}) => ( |
| 2602 | + * <Provider store={store}> |
| 2603 | + * <Pane /> |
| 2604 | + * </Provider> |
| 2605 | + * ); |
| 2606 | + * const Pane = () => { |
| 2607 | + * useTableCellIdsListener('pets', () => console.log('Cell Ids changed')); |
| 2608 | + * return <span>App</span>; |
| 2609 | + * }; |
| 2610 | + * |
| 2611 | + * const store = createStore().setTables({pets: {fido: {color: 'brown'}}}); |
| 2612 | + * const app = document.createElement('div'); |
| 2613 | + * const root = ReactDOMClient.createRoot(app); |
| 2614 | + * root.render(<App store={store} />); // !act |
| 2615 | + * console.log(store.getListenerStats().tableCellIds); |
| 2616 | + * // -> 1 |
| 2617 | + * |
| 2618 | + * store.setRow('pets', 'felix', {species: 'cat'}); // !act |
| 2619 | + * // -> 'Cell Ids changed' |
| 2620 | + * |
| 2621 | + * root.unmount(); // !act |
| 2622 | + * console.log(store.getListenerStats().rowIds); |
| 2623 | + * // -> 0 |
| 2624 | + * ``` |
| 2625 | + * @category Store hooks |
| 2626 | + */ |
| 2627 | +/// useTableCellIdsListener |
2566 | 2628 | /** |
2567 | 2629 | * The useRowIdsListener hook registers a listener function with a Store that |
2568 | 2630 | * will be called whenever the Row Ids in a Table change. |
|
0 commit comments