Send state to peers as easy as a setState() 🎣👪.
Uses peerjs under the hood,
requires a version of react/react-dom which contain hooks - at the moment this is 16.8.0-alpha.1.
npm install react-peeryarn add react-peerusePeerState(initialState?: TState, opts?: { brokerId?: string }): [TState, Function, string, Peer.DataConnection[]];
Behaves as your regular useState hook,
but will eventually send data to any connected peers.
Peers can connect to you using the brokerId that is eventually returned.
opts.brokerId is optionally used when you already have a broker id generated.
import { usePeerState } from 'react-peer';
const App = () => {
const [state, setState, brokerId, connections] = usePeerState();
setState({ message: 'hello' });
};useRecievePeerState(peerBrokerId: string, opts?: { brokerId?: string }): [TState | undefined, boolean];
Will recieve peer state eventually from a peer identified using peerBrokerId.
opts.brokerId is optionally used when you already have a broker id generated.
import { useRecievePeerState } from 'react-peer';
const App = () => {
const [state, isConnected] = useRecievePeerState('swjg3ls4bq000000');
};