Use storage functions (sending, getState) external components (external module, i.e. webSocket)

I use React and Redux, webSocket to work with some server-side events.

I can send actions from a component by assigning a function to a dispatcher using a function mapDispatchToProps().

But what about triggering outside the components? For example, with a received webSocket event.

A call store.dispatchfrom another script will return a reference error (sending unspecified), even if the repository is correctly imported

Is there any way to do this?

Here is my app store configuration:

import { createStore, combineReducers, applyMiddleware, compose } from 'Redux'
import thunk from '../../node_modules/redux-thunk/src/index'
import rootReducer  from '../reducers/index'


const configureStore = (initialState) => {
  return createStore(
    rootReducer,
    initialState,
    compose(
      applyMiddleware(thunk),
      window.devToolsExtension ? window.devToolsExtension() : f => f
    )
  )
}

export default configureStore

here is the entry point to the application where the repository is created:

import React, { Component } from 'React'
import { render } from 'ReactDOM'
import { Provider } from 'ReactRedux'
import { Router, hashHistory } from 'ReactRouter' //browserHistory

import actions  from './actions/actions'
import configureStore  from './store/configureStore'
import routes  from './routes'


const store = configureStore()
console.log('store log', store)
window.storeDebug = store.getState() // FIXME: disable in production



render(
  <Provider store={store}>
  <Router history={hashHistory} routes={routes} />
  </Provider>,
  document.getElementById('container')
)
+4
1

?

if (!window.location) {
    // App is running in simulator
    window.navigator.userAgent = 'react-native';
}

// note keep the following after the window if conditions
// https://github.com/facebook/react-native/issues/4393
const socketIO = require('socket.io-client/socket.io');

const WebSocketHandler = (store) => (next) => (action) => {

    const socket = socketIO(CHAT_SERVER_ADDRESS, {
        transports: ['websocket']
    });

    socket.on('YOUR_EVENT', (data) => store.dispatch(ACTION_CREATOR(data)));
}

configureStore

+3

All Articles