|
1016 | 1016 | * @since v1.0.0 |
1017 | 1017 | */ |
1018 | 1018 | /// useTable |
| 1019 | +/** |
| 1020 | + * The useTableState hook returns a Table and a function to set it, following |
| 1021 | + * the same pattern as React's useState hook. |
| 1022 | + * |
| 1023 | + * This is a convenience hook that combines the useTable and useSetTableCallback |
| 1024 | + * hooks. It's useful when you need both read and write access to a Table in a |
| 1025 | + * single component. |
| 1026 | + * |
| 1027 | + * A Provider component is used to wrap part of an application in a context, |
| 1028 | + * and it can contain a default Store or a set of Store objects named by Id. |
| 1029 | + * The useTableState hook lets you indicate which Store to use: omit the final |
| 1030 | + * parameter for the default context Store, provide an Id for a named context |
| 1031 | + * Store, or provide a Store explicitly by reference. |
| 1032 | + * @param tableId The Id of the Table in the Store. |
| 1033 | + * @param storeOrStoreId The Store to be accessed: omit for the default |
| 1034 | + * context Store, provide an Id for a named context Store, or provide an |
| 1035 | + * explicit reference. |
| 1036 | + * @returns An array containing the Table and a function to set it. |
| 1037 | + * @example |
| 1038 | + * This example creates a Store outside the application, which is used in the |
| 1039 | + * useTableState hook by reference. A button updates the Table when clicked. |
| 1040 | + * |
| 1041 | + * ```jsx |
| 1042 | + * import {createStore} from 'tinybase'; |
| 1043 | + * import React from 'react'; |
| 1044 | + * import {createRoot} from 'react-dom/client'; |
| 1045 | + * import {useTableState} from 'tinybase/ui-react'; |
| 1046 | + * |
| 1047 | + * const store = createStore().setTable('pets', {fido: {species: 'dog'}}); |
| 1048 | + * const App = () => { |
| 1049 | + * const [table, setTable] = useTableState('pets', store); |
| 1050 | + * return ( |
| 1051 | + * <div> |
| 1052 | + * {JSON.stringify(table)} |
| 1053 | + * <button onClick={() => setTable({...table, felix: {species: 'cat'}})}> |
| 1054 | + * Add |
| 1055 | + * </button> |
| 1056 | + * </div> |
| 1057 | + * ); |
| 1058 | + * }; |
| 1059 | + * |
| 1060 | + * const app = document.createElement('div'); |
| 1061 | + * const root = createRoot(app); |
| 1062 | + * root.render(<App />); // !act |
| 1063 | + * console.log(app.innerHTML); |
| 1064 | + * // -> '<div>{"fido":{"species":"dog"}}<button>Add</button></div>' |
| 1065 | + * |
| 1066 | + * const button = app.querySelector('button'); |
| 1067 | + * // -> button MouseEvent('click', {bubbles: true}) |
| 1068 | + * console.log(app.innerHTML); |
| 1069 | + * // -> |
| 1070 | + * ` |
| 1071 | + * <div> |
| 1072 | + * {"fido":{"species":"dog"},"felix":{"species":"cat"}} |
| 1073 | + * <button>Add</button> |
| 1074 | + * </div> |
| 1075 | + * `; |
| 1076 | + * ``` |
| 1077 | + * @category State hooks |
| 1078 | + * @since v7.3.0 |
| 1079 | + */ |
| 1080 | +/// useTableState |
1019 | 1081 | /** |
1020 | 1082 | * The useTableCellIds hook returns the Ids of every Cell used across the whole |
1021 | 1083 | * Table, and registers a listener so that any changes to that result will cause |
|
0 commit comments