Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is StoreConnector<T, Tuple2<T,T>> an anti-pattern? #32

Closed
seddonm1 opened this issue Mar 29, 2018 · 2 comments
Closed

Is StoreConnector<T, Tuple2<T,T>> an anti-pattern? #32

seddonm1 opened this issue Mar 29, 2018 · 2 comments

Comments

@seddonm1
Copy link

Coming from a ReduxJS/React background it is very common to pass multiple items from a redux store to a child component (e.g. using both mapStateToProps and mapDispatchToProps).

I have not seen in any example where you may want to pass both a state and a dispatcher to the same child component.

Is it considered correct to returning a tuple from a StoreConnector?

          // Connect the Store to a FloatingActionButton. In this case, we'll
          // use the Store to build a callback that with dispatch an Increment
          // Action.
          //
          // Then, we'll pass this callback to the button's `onPressed` handler.
          floatingActionButton:
              new StoreConnector<int, Tuple2<int, VoidCallback>>(
            converter: (store) {
              // Return a Tuple2 - one like mapStateToProps and one mapDispatchToProps
              // Return a `VoidCallback`, which is a fancy name for a function
              // with no parameters. It only dispatches an Increment action.
              return new Tuple2(
                  store.state, () => store.dispatch(Actions.Increment));
            },
            builder: (context, tuple) {
              return new FloatingActionButton(
                  // Attach the `callback` to the `onPressed` attribute
                  onPressed: tuple.item2,
                  tooltip:
                      tuple.item1.isOdd ? 'Increment_ODD' : 'Increment_EVEN',
                  child: new Icon(Icons.add));
            },
          ),
        ),
@brianegan
Copy link
Owner

Not at all! In fact, that's exactly what the ViewModel should be! A combination of State and Dispatchers.

You can see an example of this pattern here: https://github.com/brianegan/flutter_architecture_samples/blob/master/example/redux/lib/containers/todo_details.dart#L14

I generally prefer creating my own ViewModel classes instead of using Tuples, but that's just a matter of personal style.

@seddonm1
Copy link
Author

Thanks for a very fast response Brian.

I have read through that code again and it makes a lot of sense. I think a model object for its readability over TupleN is worth it alone.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants