react icon indicating copy to clipboard operation
react copied to clipboard

Improve Flux documentation

Open appsforartists opened this issue 12 years ago • 4 comments

I just read the Flux TodoMVC walkthrough. First, kudos: this is the most complete description of Flux I've seen. The last time I explored React this doc hadn't been written yet, and the existing event story ("event bubbling isn't supported - pass your listeners as callbacks through the DOM") scared me off. Flux is a much more sensible model; thanks for describing it in such detail.

I have two pieces of feedback:

we are maintaining a private data structure called _todos. … Because this variable lives outside the class, but within the closure of the module, it remains private — it cannot be directly changed from the outside. This helps us preserve a distinct input/output interface for the flow of data by making it impossible to update the store without using an action.

This is not strictly true. If your method had returned _todos.slice(), you'd be OK; but as it stands now, an oblivious caller could modify the result of TodoStore.getAll in-place without going through the store's methods. TodoStore.getAll().push(…) is effectively _todos.push(…).

  1. Move Creating Semantic Actions earlier in the walkthrough. The first thing I wonder after seeing TodoActions.destroy(this.props.todo.id); is "what does TodoActions look like?" The second thing is why are there so many layers of abstraction: wouldn't replacing that line with dispatcher.dispatch({ action, id }) do the same thing in a more direct way?

I'm glad to see the Semantic Actions section. I doubt you'd have created an additional concept (actions) if it wasn't needed, but I still couldn't help but wonder why; it's nice to see my reservation directly addressed in the documentation.

For what it's worth, I'm still not convinced that this:

TodoActions.destroy(this.props.todo.id);

is significantly cleaner/more semantic than this:

dispatcher.dispatch({
    "type": ActionType.DESTROY,
    "id": type.id
});

If both were free, I'd probably prefer the former; however, knowing that I have to write and maintain a TodoActions class (and remember what it does) adds weight to my mental model and friction to my development pace. Actions.method is a nicer syntax, but it doesn't communicate anything more clearly than dispatcher.dispatch(…) and I hate writing boilerplate.

appsforartists avatar Aug 06 '14 23:08 appsforartists

Have you seen https://github.com/facebook/flux/tree/master/examples/flux-chat? What used to be called Actions are now ActionCreators because they create Actions that get dispatched. We should update the Flux docs on the React website to reflect this.

Often times, the ActionCreators will do more than just calling the dispatcher. For example, it might make an asynchronous request to the server.

jeffchan avatar Aug 07 '14 00:08 jeffchan

Hi Brenton,

Yes, you are correct that you have access to the memory reference for _todos. This is one of the reasons that the more purist Flux implementors will always return a copy instead of the original object, or make the original object impossible to modify using ImmutableObject or similar. (see https://github.com/facebook/immutable-js)

With both flux-todomvc and flux-chat I was trying to create a simple, readable explanation of the Flux concept with enough there to fully explain the idea. Real world implementations should definitely strive to go beyond the simplicity of these examples.

Regarding the boilerplate of the action creator methods like TodoActions.destroy(): I agree there is boilerplate here. However, in a large application, this kind of code organization really does become a nice way of getting things organized. As metioned by @jeffchan, imagine a TodoActions library with 15+ methods (and also imagine 6 or 7 libraries of equal size), all with more complicated stuff like this:

updateTextByUser: function(id, text, serverPayload) {
  if (!serverPayload) {
    var currentUserID = UserStore.getCurrentID();
    AppDispatcher.handleViewAction({
      type: TodoConstants.TODO_UPDATE_TEXT,
      id: id,
      userID: currentUserID,
      text: text
    });
    TodoWebAPIUtils.updateText(id, text, currentUserID);  // make XHR call to server
  } else {
    AppDispatcher.handleServerAction({
      type: TodoConstants.TODO_UPDATE_TEXT,
      id: id,
      userID: serverPayload.userID,
      text: text
    });
  }
},

Some people would break the above into two different methods, but there are situations where maintaining the same action type for very similar actions makes sense, as does keeping it all in one action creator method. Note that we are calling handleViewAction for the client side update and handleServerAction for the data coming from the server, possibly from a different user. See the flux-chat example for an AppDispatcher that implements both handleViewAction and handleServerAction to provide different payload sources.

This sort of complexity is best kept in a very distinct place and not coded haphazardly within other modules, especially when applications grow to be quite large. It can be very helpful to see all the other actions and action creators -- how they were coded and structured -- while creating new ones. This is especially true when working with a team of engineers where you want people to stay consistent in how they do things.

fisherwebdev avatar Aug 08 '14 05:08 fisherwebdev

Bonus kudos to you all for being so responsive. I posted a couple of documentation questions yesterday and they've both been thoroughly considered by core team members within one day. Thanks guys!

(FWIW, I'll take a look at the chat example, but I still think it would be helpful if you moved the Semantic Actions section closer to the first use of TodoActions.)

appsforartists avatar Aug 08 '14 05:08 appsforartists

We're in the process of building up more Flux documentation in the https://github.com/facebook/flux repo.

Feel free to keep discussing here but I'm going to close out - I don't know how much we're going to actually do here vs the flux repo.

zpao avatar Aug 14 '14 01:08 zpao