Skip to content

A High-Order component to connect React and Socket.io easily

Notifications You must be signed in to change notification settings

PlatziDev/socket.io-react

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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