driftdb-react
Variables
DatabaseContext
• Const DatabaseContext: Context<null | DbConnection>
A React context which is used to pass a database connection down the component tree
via the DriftDBProvider provider and useDatabase hook.
Defined in
Functions
DriftDBProvider
▸ DriftDBProvider(props): React.ReactElement
A React component that provides a DbConnection to all child components.
Parameters
| Name | Type | Description |
|---|---|---|
props | Object | The props for the component. |
props.api | string | The URL of the DriftDB API. |
props.children | ReactNode | Elements under the provider in the tree. |
props.room? | string | The room ID to connect to. If not provided, attempts to extract the room ID from the URL and creates a new room if one is not present. |
props.useBinary? | boolean | Whether to use binary messages (enables raw typed arrays in messages). |
Returns
React.ReactElement
Defined in
RoomQRCode
▸ RoomQRCode(): React.ReactElement
A React component that displays a QR code containing the current URL, including the room ID. If there is no room ID in the URL, this component will not render anything.
Returns
React.ReactElement
Defined in
StatusIndicator
▸ StatusIndicator(): React.ReactElement
A React component that displays the current connection status of the database.
Returns
React.ReactElement
Defined in
useConnectionStatus
▸ useConnectionStatus(): ConnectionStatus
A React hook that returns the current connection status of the database
from the current DriftDBProvider.
The result is an object with a connected property that is true if the
database is connected to the server. When connected is true, a debugUrl
property is also returned.
Returns
ConnectionStatus
The current connection status of the database.
Defined in
useDatabase
▸ useDatabase(): DbConnection
A React hook that returns a handle to the current database connection provided by the
nearest DriftDBProvider in the tree. If there is no DriftDBProvider in the tree,
throws an error.
Returns
DbConnection
A handle to the current database connection.
Defined in
useLatency
▸ useLatency(): number | null
A React hook that measures the latency of the database connection in a
loop and returns the current latency in milliseconds, or null before
the first measurement.
Returns
number | null
Defined in
usePresence
▸ usePresence<T>(key, value): Record<string, WrappedPresenceMessage<T>>
A React hook that returns a map of the current presence of all clients in the current room. The client also passes its own value, which will be included in the map for other clients.
Type parameters
| Name |
|---|
T |
Parameters
| Name | Type | Description |
|---|---|---|
key | string | The key that uniquely identifies the presence variable within the current room. |
value | T | The value that will be included in the map for other clients. |
Returns
Record<string, WrappedPresenceMessage<T>>
A map of the current presence of all clients in the current room.
Defined in
useSharedReducer
▸ useSharedReducer<State, Action>(key, reducer, initialValue): [State, (action: Action) => void]
A React hook that returns a reducer state variable, and a function to update it. The state variable is identified by a key, which must be unique within the current room.
Type parameters
| Name |
|---|
State |
Action |
Parameters
| Name | Type | Description |
|---|---|---|
key | string | The key that uniquely identifies the state variable within the current room. |
reducer | (state: State, action: Action) => State | A reducer function that will be used to update the state variable. |
initialValue | State | The initial value of the state variable (if init is not passed), or the value passed into init to produce the initial value. |
Returns
[State, (action: Action) => void]
Defined in
▸ useSharedReducer<State, Action, InitialValue>(key, reducer, initialValue, init): [State, (action: Action) => void]
A React hook that returns a reducer state variable, and a function to update it. The state variable is identified by a key, which must be unique within the current room.
Type parameters
| Name |
|---|
State |
Action |
InitialValue |
Parameters
| Name | Type | Description |
|---|---|---|
key | string | The key that uniquely identifies the state variable within the current room. |
reducer | (state: State, action: Action) => State | A reducer function that will be used to update the state variable. |
initialValue | InitialValue | The initial value of the state variable (if init is not passed), or the value passed into init to produce the initial value. |
init | (initialValue: InitialValue) => State | An optional function that will be used to produce the initial value of the state variable. |
Returns
[State, (action: Action) => void]
Defined in
useSharedState
▸ useSharedState<T>(key, initialValue): [T, SetterFunction<T>]
A React hook that returns the current value of a shared state variable, and a function to update it. The state variable is identified by a key, which must be unique within the current room.
Type parameters
| Name |
|---|
T |
Parameters
| Name | Type | Description |
|---|---|---|
key | string | The key of the state variable. |
initialValue | T | The initial value of the state variable. |
Returns
[T, SetterFunction<T>]
A tuple containing the current value of the state variable, and a function to update it.
Defined in
useUniqueClientId
▸ useUniqueClientId(): string
A React hook that returns a unique client ID for the current client. This ID is maintained in the browser’s session storage, so it is retained across page reloads.
Returns
string
A unique client ID.
Defined in
useWebRtcPresence
▸ useWebRtcPresence<T>(value, throttle?): Record<string, WrappedPresenceMessage<T>>
A React hook that returns a map of the current presence of all clients in the current room. The client also passes its own value, which wil be included in the map for other clients
Type parameters
| Name |
|---|
T |
Parameters
| Name | Type | Default value | Description |
|---|---|---|---|
value | T | undefined | The value that will be included in the map for other clients. |
throttle | number | 0 | The minimum interval between messages being sent. NOTE: any messages sent in the interval will be dropped. |
Returns
Record<string, WrappedPresenceMessage<T>>
A map of the current presence of all clients in the current room.