Reswift: Action vs ActionCreator vs AsyncActionCreator

Created on 11 Sep 2016  路  3Comments  路  Source: ReSwift/ReSwift

Having come from using redux, I have a few questions regarding the ReSwift types and how they map to redux.

Within redux, Actions are simply objects with the correct 'shape', i.e. a type and a payload, and ActionCreators are functions that create these Actions.

With that in mind, I do wonder whether the ReSwift Action is actually an ActionCreator? Looking at the code for a simple Action that adds an item to a to-do list:

struct AddToDo: Action {
    let text: String

    init(text: String) {
        self.text = text
    }
}

This is both an Action (it has a type and a payload), and an ActionCreator (it has an initialiser that constructs the Action).

This might sound very pedantic, but here's where I think ReSwift starts to deviate ...

The ReSwift ActionCreator type has state and store parameters. This is quite different from redux:

In general accessing state in action creators is an anti-pattern and you should avoid this when possible.

from Dan Abramov - http://stackoverflow.com/questions/35667249/accessing-redux-state-in-an-action-creator

Again, this might all sounds a little pedantic, but it does lead to some real confusion when trying to understand how to dispatch actions asynchronously. I'd expect this to be the role of the AsyncActionCreator, however, you can also do this (more easily) via ActionCreator where you returns nil, and instead dispatch to the store. I've noticed this being done in the following example:

https://github.com/Ben-G/Meet/blob/master/Meet/Meet/Actions/TwitterAPIActionCreator.swift#L58

I do wonder whether ReSwift would also benefit from using the same pattern as redux where, rather than providing functions with the store, they instead provide a 'view' onto the store which just allows them to dispatch or invoke getState

Sorry - a bit of a messy brain-dump here. I'd be happy to turn this into something more concrete if you're interested?

Needs Discussion

Most helpful comment

@ColinEberhardt just confirming that I did read this post. Had a crazy week due to Xcode 8 / iOS 10 related fixes. You're bringing up good points. Want to get back to them latest over weekend.

All 3 comments

@ColinEberhardt just confirming that I did read this post. Had a crazy week due to Xcode 8 / iOS 10 related fixes. You're bringing up good points. Want to get back to them latest over weekend.

No worries, thanks for letting me know!

Closing this with #231 with the recent addition of ReSwift-Thunk to the ecosystem (e.g. #372 and #378)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

krema picture krema  路  6Comments

MichaelGuoXY picture MichaelGuoXY  路  7Comments

adamhongmy picture adamhongmy  路  3Comments

coderMemberam picture coderMemberam  路  8Comments

dolanmiu picture dolanmiu  路  3Comments