useSynchronizerStatus
The useSynchronizerStatus
hook returns a the status of a Synchronizer
, and registers a listener so that any changes to it will cause a re-render.
useSynchronizerStatus(synchronizerOrSynchronizerId?: SynchronizerOrSynchronizerId): Status
Type | Description | |
---|---|---|
synchronizerOrSynchronizerId? | SynchronizerOrSynchronizerId | The |
returns | Status | The status of the |
A Provider
component is used to wrap part of an application in a context, and it can contain a default Synchronizer
or a set of Synchronizer
objects named by Id
. The useSynchronizerStatus
hook lets you indicate which Synchronizer
to get data for: omit the optional parameter for the default context Synchronizer
, provide an Id
for a named context Synchronizer
, or provide a Synchronizer
explicitly by reference.
When first rendered, this hook will create a listener so that changes to the Synchronizer
status will cause a re-render. When the component containing this hook is unmounted, the listener will be automatically removed.
Examples
This example creates a Synchronizer
outside the application, which is used in the useSynchronizerStatus
hook by reference. A change to the status of the Synchronizer
re-renders the component.
import React from 'react';
import {createLocalSynchronizer} from 'tinybase/synchronizers/synchronizer-local';
import {createMergeableStore} from 'tinybase';
import {createRoot} from 'react-dom/client';
import {useSynchronizerStatus} from 'tinybase/ui-react';
const synchronizer = createLocalSynchronizer(createMergeableStore());
const App = () => <span>{useSynchronizerStatus(synchronizer)}</span>;
const app = document.createElement('div');
const root = createRoot(app);
root.render(<App />);
console.log(app.innerHTML);
// -> '<span>0</span>'
This example creates a Provider context into which a default Synchronizer
is provided. A component within it then uses the useSynchronizerStatus
hook.
import {Provider, useSynchronizerStatus} from 'tinybase/ui-react';
import React from 'react';
import {createLocalSynchronizer} from 'tinybase/synchronizers/synchronizer-local';
import {createMergeableStore} from 'tinybase';
import {createRoot} from 'react-dom/client';
const App = ({synchronizer}) => (
<Provider synchronizer={synchronizer}>
<Pane />
</Provider>
);
const Pane = () => <span>{useSynchronizerStatus()}</span>;
const synchronizer = createLocalSynchronizer(createMergeableStore());
const app = document.createElement('div');
createRoot(app).render(<App synchronizer={synchronizer} />);
console.log(app.innerHTML);
// -> '<span>0</span>'
This example creates a Provider context into which a Synchronizer
is provided, named by Id
. A component within it then uses the useSynchronizerStatus
hook.
import {Provider, useSynchronizerStatus} from 'tinybase/ui-react';
import React from 'react';
import {createLocalSynchronizer} from 'tinybase/synchronizers/synchronizer-local';
import {createMergeableStore} from 'tinybase';
import {createRoot} from 'react-dom/client';
const App = ({synchronizer}) => (
<Provider synchronizersById={{petSynchronizer: synchronizer}}>
<Pane />
</Provider>
);
const Pane = () => <span>{useSynchronizerStatus('petSynchronizer')}</span>;
const synchronizer = createLocalSynchronizer(createMergeableStore());
const app = document.createElement('div');
createRoot(app).render(<App synchronizer={synchronizer} />);
console.log(app.innerHTML);
// -> '<span>0</span>'
Since
v5.3.0