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.

Support nesting for connect mapDispatchToProps?

See original GitHub issue

Example:

connect(() => {...}, {
  noteActions: {
    create: createNote,
    update: updateNote,
    ...
  },
  laneActions: {
    create: createLane
  }
})(Demo);

// at component
this.props.noteActions.create();

This would allow you to namespace actions better. You could even do

connect(() => {...}, {
  noteActions,
  laneActions
})(Demo);

if you want to be terse and don’t mind connecting each action. This isn’t as maintainable, though.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:2
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

5reactions
gaearoncommented, Oct 1, 2015

I’d be wary of including too many convenient opinionated defaults. I generally agree namespacing is nice, but there are potential unwanted side effects too (e.g. an export of some unrelated object from actions.js that we begin to iterate over because of import * as actions—or a recursive object we get stuck on).

The example in API docs actually shows namespacing:

import * as todoActionCreators from './todoActionCreators';
import * as counterActionCreators from './counterActionCreators';
import { bindActionCreators } from 'redux';

function mapStateToProps(state) {
  return { todos: state.todos };
}

function mapDispatchToProps(dispatch) {
  return {
    todoActions: bindActionCreators(todoActionCreators, dispatch),
    counterActions: bindActionCreators(counterActionCreators, dispatch)
  };
}

export default connect(mapStateToProps, mapDispatchToProps)(TodoApp);

I don’t think it’s too much typing, and the upside is it’s explicit and non-magic. I’d prefer to keep it that way.

0reactions
tounanocommented, Oct 1, 2015

I think the way it is right now is perfect.

This of mapDispatchToProps as factory.

You can do something like this:

function attachBookActions (dispatch, getState, actions) {
  return assign({}, actions, {bookActions: {getBook: function () {//...}}}
}

function attachAuthorActions (dispatch, getState, actions) {
  return assign({}, actions, {authorActions: {getAuthor: function () {//...}}}
}

function combinePropMappers (factories, dispatch, getState) {
  return factories
          .map(function (factory) {
            return factory.bind(null, dispatch, getState)
          })
          .reduce(function (actions, factory) {
            return factory(actions);
          }, {});
}

var mapDispatchToProps = combinePropMappers.bind(null, [
    attachAuthorActions,
    attachBookActions
  ]);

You can achieve a cleaner code with currying. That’s the way I try to write all my apps. I try not to create objects out of nowhere. only functions that reduce other objects.

This gives you the advantage of separating reducers.

For example you’d be able to have book reducers and author reducers.

All contained in the same module. Each module would have it’s factories. Or whatever functions that are related to the logic of the reducer.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Connect | React Redux
Overview​. The connect() function connects a React component to a Redux store. It provides its connected component with the pieces of the ...
Read more >
react redux connect in nested components - Stack Overflow
When nesting the SubCategoryDropDown in the CategoryTypeDropDown Component only the CategoryDropDown connect() method gets triggered. // This is ...
Read more >
How to Nest Smart Components in Redux | Pluralsight
Nesting smart components is a popular design pattern for building ... the connect() method and secure its props using mapStateToProps() .
Read more >
A Look at the Redux connect() Function | by Akhil Reddy Mallidi
Redux's connect() function is a Higher-Order Function that takes two functions as parameters (mapStateToProps and mapDispatchToProps), and it also returns a ...
Read more >
Why using nested connect(react-redux) components is good?
Now to help parent you can connect a child and let it extract its own props and props for its children. That way...
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