0

How can I change the state using socket?

socket.js

import {getNearbyUser} from '../routes/Search/modules/Search'
export function socketAction(action,data){
    if(action == 'login'){
        socket.emit('login',data);
    }
    else if(action == 'book'){
        socket.emit('book',data);
    }

    socket.on('nearbyUser', getNearbyUser);
}

Search.js

export function getNearbyUser(data){
    alert(data.latitude + '\n' + data.longitude);
    return (dispatch) => {
        dispatch({
            type: GET_NEARBY_HANDYMAN,
            address: data
        })
    }
}

server.js

socket.on('handyman',(data) => {
    console.log(data);
    socket.to('customerRoom').emit('nearbyHandyman',data);
})

It alerts the latitude and longitude but the state is not changing or it's not dispatching.

PJ Concepcion
  • 108
  • 1
  • 13

1 Answers1

0

getNearbyUser returns a function which is never being called. Try:

socket.on('nearbyUser', data => getNearbyUser(data)(dispatch));

Not sure where dispatch is defined but you'll need to pass that in as well.

Alan Friedman
  • 1,582
  • 9
  • 7