S
S
SweetPony12017-10-06 14:43:49
JavaScript
SweetPony1, 2017-10-06 14:43:49

How to subscribe to events from a component in Redux?

How to subscribe to events from a component in Redux?
CREATE-REACT-APP is used for development.
All the answers we have found are about a more simplified project structure.

store.subscribe(() => {
  console.log(store.getState().lastAction);
});

But I have a project of this type
https://monsterlessons.com/project/categories/redux
The main task of subscribing to dispatch type is that actions from different files have no idea about the existence of each other.
EVENT1 and EVENT2 are launched from different files, import EVENT2 in EVENT1 is a wrong decision, all the more it interferes with dispatch =>
With such a project scheme, I can’t find where to subscribe to changes.
I made a simplified
index.js example for the forum
import React from 'react'
import ReactDOM from 'react-dom'
import { Provider } from 'react-redux'
import { createStore, applyMiddleware } from 'redux'
import { composeWithDevTools } from 'redux-devtools-extension'
import thunk from 'redux-thunk'
import { Router, Route, hashHistory } from 'react-router'
import { syncHistoryWithStore } from 'react-router-redux'
import App from './App'
import reducer from './reducers'
const store = createStore(reducer, composeWithDevTools(applyMiddleware(thunk)));
const history = syncHistoryWithStore(hashHistory, store);
ReactDOM.render(
  <Provider store={store}>
    <Router history={history}>
      <Route path="/" component={App} />
    </Router>
  </Provider>,
  document.getElementById('root')
);

reducers
export default function reducer1(state = initialState, action) {
  switch (action.type) {
    default: {
      return state;
}

action1
export const action1 = (data) => dispatch => {
  dispatch({ type: 'EVENT1', payload: {value1:1} })
}

action2
export const action2 = (data) => dispatch => {
  dispatch({ type: 'EVENT2', payload: {value2:2} })
}

Component
import * as action1 from './actions/action1';
import * as action2 from './actions/action2';
class App extends Component {
  componentDidMount() {
      this.props.state.reducer1.subscribe( ()=>{
       /*мои представления о работе подписки, тут мы имеем доступ к EVENT1 и EVENT2*/
      } )
  }
  render() {
      return (<div></div>)
  }
}
export default connect(
    (state, props, ownProps) => ({
        state: state,
        props: props
    }),
    actions1,actions2
)(App)

but in my case
this.props.state.reducer1.value1
doesn't have functions like getState() and subscribe()

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Alekseev, 2018-04-03
@Zatmil

It seems to me that you need to look towards the middleware and make a wrapper in which you can already call the necessary actions

const MyMiddleware = store => next => action => {
  switch(action.type) {
    case 'SOME_EVENT_TYPE': {
       store.dispatch(action)
    }
    default:
      return next(action)
  }
}

M
Maxim Gatilin, 2018-08-15
@gatilin222

You are going the wrong way, you have an architectural error. Why subscribe from a component to an action? Let the action change something in the Redux store and the updated data comes to the component.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question