Skip to main content

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

index.tsx:89

Functions

DriftDBProvider

DriftDBProvider(props): React.ReactElement

A React component that provides a DbConnection to all child components.

Parameters

NameTypeDescription
propsObjectThe props for the component.
props.apistringThe URL of the DriftDB API.
props.childrenReactNodeElements under the provider in the tree.
props.room?stringThe 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?booleanWhether to use binary messages (enables raw typed arrays in messages).

Returns

React.ReactElement

Defined in

index.tsx:33


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

index.tsx:159


StatusIndicator

StatusIndicator(): React.ReactElement

A React component that displays the current connection status of the database.

Returns

React.ReactElement

Defined in

index.tsx:418


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

index.tsx:276


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

index.tsx:98


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

index.tsx:298


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

NameTypeDescription
keystringThe key that uniquely identifies the presence variable within the current room.
valueTThe 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

index.tsx:386


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

NameTypeDescription
keystringThe key that uniquely identifies the state variable within the current room.
reducer(state: State, action: Action) => StateA reducer function that will be used to update the state variable.
initialValueStateThe 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

index.tsx:203

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

NameTypeDescription
keystringThe key that uniquely identifies the state variable within the current room.
reducer(state: State, action: Action) => StateA reducer function that will be used to update the state variable.
initialValueInitialValueThe 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) => StateAn optional function that will be used to produce the initial value of the state variable.

Returns

[State, (action: Action) => void]

Defined in

index.tsx:209


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

NameTypeDescription
keystringThe key of the state variable.
initialValueTThe 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

index.tsx:119


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

index.tsx:190


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

NameTypeDefault valueDescription
valueTundefinedThe value that will be included in the map for other clients.
throttlenumber0The 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.

Defined in

index.tsx:366