9

ReactJS and Redux offer a new paradigm as far as developing the front end of applications goes. Both have relatively simple APIs and after spending a little time are pretty easy to grasp from a technical point of view. But from a design perspective, I have been unable to find recommendations on best practices or pitfalls.

Where can I find recommendations or advice on how to design my global Redux store? Keeping the entire state of an application in a global store seems like it could get unwieldy rapidly. What should I be sure to avoid in doing so? What about the actions that modify the global state? Is it better to make fewer actions that can do different things based on the action data, or many more specific actions?

Joseph
  • 12,678
  • 19
  • 76
  • 115

1 Answers1

8

This is a great question, but it's a bit difficult to tangibly answer because so much of it is an "it depends" question. But, I would highly recommend taking an in-depth look at the redux.js.org docs. There's lots of little bits of wisdom in there about suggestions for shaping an API or reducing duplication or general unwieldiness.

Additionally, I'd add a few general tips:

  • you usually don't need state quite as often as you'd think. So some things can be accomplished with more props and less store-driven state
  • grouping things into larger "topics" can often help data organization at the store level; so you might provide higher-level properties on the store for a given user, auth, and any further "topics" would get grouped in the same way.
  • sometimes, you might create slightly-more verbose state structures, but the tradeoff is predicability and greater overall simplicity. Still a win over MVC or 2-way craziness
  • last, as a minor point, "global" state doesn't really have to be so global. In one sense, a single state tree means that all the state is "global" (or at least unified), but unless you expose the store to every single component, it's not really global. Nicely-decoupled components will make thinking about data flowing from up-top easier IMO and things don't really have to be "global", if that makes sense.

Hope that helps a bit!

See also:

markthethomas
  • 4,391
  • 2
  • 25
  • 38
  • 1
    Good answer. To piggy-back on this: I have a collection of links to React/Redux best practices and guidelines over at https://github.com/markerikson/react-redux-links/blob/master/tips-and-best-practices.md , and I believe some of those discuss state management. – markerikson Mar 26 '16 at 00:50
  • @markthethomas can you help me with the below problem http://stackoverflow.com/questions/36211739/invariant-violation-could-not-find-store-in-either-the-context-or-props-of-c/36214384?noredirect=1#comment60079395_36214384 –  Mar 26 '16 at 02:47
  • I can take a look. I suggest switching to enzyme if possible for testing; so far I've had a really nice experience with it. See https://semaphoreci.com/community/tutorials/testing-react-components-with-enzyme-and-mocha and https://airbnb.io/enzyme/ – markthethomas Mar 26 '16 at 02:59