socket.io-react
v1.2.0
Published
A High-Order Component to connect React and Socket.io
Downloads
1,552
Readme
socket.io-react
A High-Order component to connect React and Socket.io easily.
API
import {
SocketProvider,
socketConnect,
} from 'socket.io-react';
SocketProvider(socket?)
import { SocketProvider } from 'socket.io-react';
import io from 'socket.io-client';
import App from './containers/App';
const socket = io.connect(process.env.SOCKET_URL);
socket.on('message', msg => console.log(msg));
const DOMNode = document.getElementById('renderTarget');
render(
<SocketProvider socket={socket}>
<App />
</SocketProvider>,
DOMNode
);
socket
property isfalse
by default.
socketConnect(Target)
import { socketConnect } from 'socket.io-react';
function App(props) {
function sendMessage() {
props.socket.emit('message', 'Hello world!');
}
return (
<button onClick={sendMessage}>
Send!
</button>
);
}
export default socketConnect(App);
socketConnect
can be used as a decorator (using babel-plugin-transform-decorators-legacy)