Unleash awesomeness. Private packages, team management tools, and powerful integrations. Get started with npm Orgs »

socket.io-react

1.2.0 • Public • Published

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 is false 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);

install

npm i socket.io-react

Downloadsweekly downloads

1,093

version

1.2.0

license

MIT

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
  • avatar
  • avatar
Report a vulnerability