openrtc-netcode-react
v3.3.1
Published
React context and hooks for openrtc-netcode.
Maintainers
Readme
openrtc-netcode-react
React context and hooks for an explicitly activated openrtc-netcode client.
import { NetcodeProvider, useNetVar } from 'openrtc-netcode-react';
function Score() {
const [score, setScore] = useNetVar('score', 0);
return <button onClick={() => void setScore(score + 1)}>{score}</button>;
}
// Construct and join client with your activated OpenRTC Room first.
export function Game({ client }) {
return <NetcodeProvider client={client} autoStart={false}><Score /></NetcodeProvider>;
}useNetVar follows key/client changes and state reset without retaining retired
match handles. Ownership still applies; a non-owner setter rejects. Provider
auto-start errors go to onError (or console.error), including cleanup errors.
Keep a supplied client and options stable across ordinary renders.
For high-frequency 3D rendering, use useNetworkObjectIds(kind) for structure
and client.objects.read(id) in the engine frame loop; sample a SnapshotBuffer
into pooled vectors. This avoids collection clones and React renders for motion.
useNetworkObject(id) and useNetworkObjects(kind) suit lower-frequency UI.
Objects returned by imperative read and hooks are deeply frozen. Use object
mutation APIs rather than changing nested fields.
Wire v2 requires the coordinated major release; see core netcode migration, authority, bounds and validation documentation.
