Razvan Alex
Razvan Alex

Reputation: 1800

Socket.emit and Socket.on translated to react js

I'm looking to make a chat app with react and socket io. but I really don't know how to mix the socket.io syntaxes with react. I'm mostly looking to know how to implement socket.emit and socket.on on client side with react. So far I got the socket.io-client side module and heard that they should be included in componenetDidMount, ty!

Upvotes: 0

Views: 625

Answers (2)

John
John

Reputation: 2894

React's philosophy is to make your page view a (render) function of your data (state). If you would like to incorporate different data, you simply need to integrate it with React's state. So in componentDidMount, set up the socket's event handlers to call setState, and React will re-render with the new state.

Upvotes: 0

pro
pro

Reputation: 792

import socket.io...

Inside your class

  componentDidMount(){
      socket.on('someEvent', function(data){
        //do something with the data
      });
   }

I am assuming you have

socket.emit('someEvent', {lang: 'js'});

somewhere

Upvotes: 2

Related Questions