Search code examples
design-patternssingletonreactjsglobalflux

React patterns without globals/singletons


I've seen a few comments from other people who are also wary of using global dispatchers and stores as advocated by Flux. I'm wondering, what alternative design patterns have you used?

Update

After becoming more familiar with how react-redux works, I realized that it doesn't involve a global singleton at all, but rather just puts a store on React context, which I think is a fantastic solution.


Solution

  • Flummox is a nice Flux implementation that doesn't rely on singleton stores/dispatcher.

    The issue with singletons is mostly if you want to run React on the server. If you're only running on the client, it's a fair assumption that a store/dispatcher only ever serves one user. So you can assume that the state in a store is state that belongs to that user. But you can't make that assumption on the server, since the same singleton will be used for multiple requests/users. You can still use singletons on the server though, but you have to pass user id to all actions and methods on the stores.