|
397 | 397 | * @since v1.0.0 |
398 | 398 | */ |
399 | 399 | /// useStore |
| 400 | +/** |
| 401 | + * The useStores hook is used to get a reference to all the Store objects named |
| 402 | + * by Id within a Provider component context. |
| 403 | + * |
| 404 | + * A Provider component is used to wrap part of an application in a context. It |
| 405 | + * can contain a default Store (or a set of Store objects named by Id) that can |
| 406 | + * be easily accessed without having to be passed down as props through every |
| 407 | + * component. |
| 408 | + * |
| 409 | + * The useStores hook lets you get a reference to the latter as an object. |
| 410 | + * @returns An object containing all the Store objects named by Id. |
| 411 | + * @example |
| 412 | + * This example creates a Provider context into which a Store is provided, named |
| 413 | + * by Id. A component within it then uses the useStores hook to get a reference |
| 414 | + * to the Store again. |
| 415 | + * |
| 416 | + * ```jsx |
| 417 | + * import {Provider, useStores} from 'tinybase/ui-react'; |
| 418 | + * import React from 'react'; |
| 419 | + * import {createRoot} from 'react-dom/client'; |
| 420 | + * import {createStore} from 'tinybase'; |
| 421 | + * |
| 422 | + * const App = ({store}) => ( |
| 423 | + * <Provider storesById={{petStore: store}}> |
| 424 | + * <Pane /> |
| 425 | + * </Provider> |
| 426 | + * ); |
| 427 | + * const Pane = () => ( |
| 428 | + * <span>{useStores()['petStore'].getListenerStats().tables}</span> |
| 429 | + * ); |
| 430 | + * |
| 431 | + * const store = createStore(); |
| 432 | + * const app = document.createElement('div'); |
| 433 | + * createRoot(app).render(<App store={store} />); // !act |
| 434 | + * console.log(app.innerHTML); |
| 435 | + * // -> '<span>0</span>' |
| 436 | + * ``` |
| 437 | + * @category Store hooks |
| 438 | + * @since v5.4.1 |
| 439 | + */ |
| 440 | +/// useStores |
400 | 441 | /** |
401 | 442 | * The useStoreOrStoreById hook is used to get a reference to a Store object |
402 | 443 | * from within a Provider component context, _or_ have it passed directly to |
|
0 commit comments