1.0.0 • Published 5 years ago
@santosmarco/use-socket-io v1.0.0
use-socket-io
Access the client-side Socket instance of your app with the ease of a single React hook.
Installation
With npm:
npm i --save use-socket-ioWith yarn:
yarn add use-socket-ioUsage
import SocketProvider, { useSocket } from "use-socket-io";
const App = () => {
const socket = useSocket();
return <div>App</div>;
};
render(
<SocketProvider uri="http://localhost:8080" autoConnect>
<App />
</SocketProvider>,
document.getElementById("root")
);API reference
SocketProvider
The wrapper component for the useSocket hook.
Props:
uristring REQUIRED – Socket's URI.autoConnectboolean – Whether the component should automatically connect to the Socket upon mounting. Default:falseonConnectfunction – An optional callback function to run upon connection.onDisconnectfunction – An optional callback function to run upon disconnection.
Example:
<SocketProvider uri="http://localhost:8080" autoConnect>
<App />
</SocketProvider>useSocket
A React hook to access the Socket instance anywhere throughout your app.
See https://socket.io/docs/v3/client-api/index.html#Socket for a detailed description of the Socket instance.
Must be called from a child of <SocketProvider />.
1.0.0
5 years ago