K
K
Keppp2019-09-30 21:39:53
React
Keppp, 2019-09-30 21:39:53

Where is it correct to register connect if possible?

It is necessary for me that when the answer came to the server, the setUsersData action was called. Where do I need to write connect( null, mapDispatchToProps) that it was available in onmessage?

import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';

import { usersActions } from 'reducers';

const mapDispatchToProps = dispatch =>
  bindActionCreators(
    {
      setUsersData: usersActions.setUsersData
    },
    dispatch
  );

export default ((wsUrl) => {
  let ws;

  ws = new WebSocket(wsUrl);

  ws.onopen = () => {
    console.log('WS open');
  };

  ws.onmessage = (message) => {
    console.log(message.data);
    // setUsersData(message.data);
  };

  const emit = (message) => {
    if (ws.readyState !== ws.CONNECTING) {
      ws.send(message);
    }
  };

  return { emit };
})('ws://localhost:3000');

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
abberati, 2019-09-30
@Keppp

Nowhere
For side effects (your case is just a side effect), middlewares should be used - google redux-saga, this library will suit you.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question