reduxjs / redux-toolkit

The official, opinionated, batteries-included toolset for efficient Redux development
https://redux-toolkit.js.org
MIT License
10.71k stars 1.17k forks source link

Feature: State watcher #170

Closed AlaaZorkane closed 5 years ago

AlaaZorkane commented 5 years ago

Hi, great work on the redux-starter-kit, really loving it!

My requested feature is a state watcher utility, something like subscribe() but on a higher-level. While it is entirely possible to make it your own, it's very hard for beginners to not mess it up. And currently there is no function that lets you observe the store for changes, and as stated in the docs, it's not practical to use subscribe() directly as stated here https://stackoverflow.com/questions/36212860/subscribe-to-single-property-change-in-store-in-redux

So maybe, to help non-advanced users, a utility function to watch (or observe) either a single property in the state or the whole state.

EDIT: I'd like to add while this still exists https://github.com/jprichardson/redux-watch , it's better to have an updated version of it which adds more features and a better syntax maybe.

markerikson commented 5 years ago

It's an interesting idea, but I don't think it's something I want to add directly to RSK.

As you noted, redux-watch exists, and there's several other similar libraries as well:

https://github.com/markerikson/redux-ecosystem-links/blob/master/store.md#store-change-subscriptions

Most of the time, store state changes are only needed in components, in which case you know use mapState or useSelector and know that it's changed when the component re-renders.

Out of curiosity, what specific use case do you have for needing to watch changes to a given piece of state?

AlaaZorkane commented 5 years ago

I'm using Redux on the server-side of a data-driven framework, I have a lot of modules and it's easier to communicate all that data using redux (and eventually rsk), my server opens a websocket to be used on the client via an interface or anything (to monitor the data). Since the store holds a lot of data, and a big chunk of that data doesn't actually update, I wanted to observe a set of properties that are frequently changing to not overload the client and only send what changed IF changed.

I understand that on React, most of the time store state changes are indeed only needed in components. While this is completely true, it's important to remember that Redux is gaining a big popularity in the backend as an alternative for the classic pub-sub system, and it's lacking the tools that react-redux offers.

markerikson commented 5 years ago

Hmm. To be honest, I haven't seen or heard much about Redux being used in the back end, so I'm not nearly as familiar with that use case.

I don't immediately know of a good addon that would produce a diff of just the changed fields. One might exist, and there might even be some in that list I linked, but if so it's not something I specifically remember seeing off the top of my head.

I think building one would be entirely feasible, but I also don't see that as something that needs to be added to RSK directly, so I'll go ahead and close this issue. If you do find or build something like that, feel free to comment here to let me know.