Thank you for your feedback and time spent with these tools. I appreciate it.

In larger and more complicated applications, do you see react-capsule act as a supplement to the global god state?

I absolutely think the way forward in large applications is a mix of global state solutions, where each slice of global state uses the tool best appropriate for solving the problem. My largest product currently uses Redux, ReactN, and the “ECMAScript module” state mentioned in this article. Those module states would be better suited to be (and were the inspiration for) capsules. There are local states in the application that would benefit from being shared but haven’t been because the refactoring of a global god state has been too much effort to bother. As they say, “When all you have is a hammer, everything look like a nail.” Unfortunately Redux is a bulldozer and all we need is a shovel. The intention is that now capsules can be that shovel. Some slices of state may only need a shovel while other slices may need a bulldozer. The appropriate available tooling provides the best developer experience and application safety (maintainability, testability, confidence, etc.).

is the benefit of using capsule.subscribe() compared to a useEffect() depending on capsule.state?

The subscribe and unsubscribe methods are for extensibility. These methods are used internally by the capsule.useState() hook, subscribing and unsubscribing the component to state changes. For developer convenience, they are also made public, in case developers want to build tools on top of capsules. For example, the developer may want to subscribe to state changes in order to copy the state to local storage, using the local storage value as the initial state for the capsule.

const capsule = new Capsule(localStorage.getItem('thisSlice'));
capsule.subscribe(() => {
localStorage.setItem('thisSlice', capsule.state);
});

Conceptually, the capsule is the most bare-bones building block for tying a shared state to a component’s life cycle. Other tools, like ReactN or Redux, could be built on top of it. Those additional features are not shipped out-of-the-box, because that leads to the boilerplate and difficult in maintainability that comes with using a bulldozer when you only need a shovel.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store