question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Problem dispatching actions from different components

See original GitHub issue

First of all, thankyou @wellyshen for the great boilerplate.

I have the following structure from a checkout of the boilerplate 5 days ago.

        /Tags/
             /index.js //component
             /reducer.js // the reducer for that component
             /actions.js // all the actions that can be performed by that component
       /Products/
             /index.js
             /reducer.js 
             /actions.js 

I’m trying to have a component connect to two reducers.

I use redux’s connect and mapStateToProps in this way:

const mapStateToProps = state => (
    { products: state.get('products').products, tags: state.get('tags').tags }
  );
  
  export default connect(mapStateToProps)(Products);

Now, I’d like to dispatch from the very same component two actions. One action is from the actions in the same component folder, and the other is from the folder in tags. The name of the actions are unique. The actions are executed correctly and both try to update their own reducers with dispatch(). The reducer of tags doesn’t receive his own dispatch (it doesn’t see it), while the reducer of Products, receives both.

I believe i’m doing a bit (lot) of confusion with the function dispatch that seams to be the same and coming from the component itself. Actions that are dispatched from the Tags component are processed correctly by its reducer.

Any suggestion?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
nicolabortignoncommented, Jan 14, 2017

Ok, so I had to rewrite the injection of the reducer in the router.js.

Not the most elegant but here it is:

`{ path: ‘/products’, getComponent(nextState, cb) { const importModules = Promise.all([ System.import(‘./containers/Products’), System.import(‘./containers/Products/reducer’), System.import(‘./containers/Tags/reducer’), ]);

      const renderRoute = loadModule(cb);

      importModules
        .then(([Component, ...reducers]) => {
          console.log('reducer');
          console.log(reducers);
          injectReducer(store, 'products', reducers[0].default);
          injectReducer(store, 'tags', reducers[1].default);
          renderRoute(Component);
        })
        .catch(errorLoading);
    },
  },

`

Ideally would be nice if the system.import can detect the name of the reducer, and the injectReducer calls is called on every element of the array reducers… but oh well. for now at least it works 😃

0reactions
wellyshencommented, Jan 16, 2017

@nicolabortignon

Just pass the reducer as the prop from the top level container.

By the way if you have a container inside a container, then you also can access the reducer through the redux connect from the children container directly, because the reducer of the container already be injected while the route be injected 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Dispatching Actions from Child Components | by Diganta Das
In a React-Redux setup, we have containers which connects to store using connect annotation. The container contains views or other child ...
Read more >
Can I dispatch redux action in separate from react component?
1 Answer 1 ... In a React/Redux app, you should not dispatch actions outside of React components. And React components should only dispatch...
Read more >
Can't Dispatch Redux Action From Outside of Component ...
Hello! I am having an issue where I am unable to dispatch actions from outside of a component. I have found many other...
Read more >
Connect: Dispatching Actions with mapDispatchToProps
Providing a mapDispatchToProps allows you to specify which actions your component might need to dispatch. It lets you provide action dispatching ...
Read more >
React Redux: performance considerations when dispatching ...
In a React Redux app, what happens when you dispatch multiple actions in a row? When used out of the box without any...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found