-
-
Notifications
You must be signed in to change notification settings - Fork 15.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enhancers extending state will not work for all reducers #3773
Comments
As a store enhancer, they have quite a bit of control over the behavior and shape of the resulting store. They may want to add things to state related to the things they are enhancing. I could see a WebSocket enhancer that exposes the global connection instance via getState(). The main reason for doing that would be to allow access during connect() or useSelector(). So, I think it should stay. |
But how would the WebSocket enhancer add to the state if the state is just a number or a string? |
It seems to me like the preferable way for a library to extend state would be to expose a reducer and an enhancer and have the user provide the reducer in Also, if the enhancer mixes in the state themselves, it seems like it would be hard to make sure that the key for that state isn't already used. It would be easier to ensure that if the user has to mix in the reducer themselves. Finally, are there are any real-life examples of enhancers that extend state? It would be helpful to see what that would look like to help inform the conversation. |
Also worth noting that this is really a conversation about types. If an enhancer really wants to extend state, they can write the JavaScript to do so. However, providing a Removing the If a library wants to be able to extend the state using TypeScript, they can expose a |
They would need to handle it in the setup function, I would imagine. Simple state values aren't typically used with enhancers like that. I would say that's an extreme edge case. |
Still seems like it wouldn't be too hard for an enhancer to just have the user add the reducer themselves. It will also prevent the enhancer from having to deal with the edge cases where the state isn't an object or the key already exists in the reducer. It could be implemented like this:
In summary:
Since it doesn't seem very common, it simplifies the Redux types, and there's better type-safety all around, I still think it's a net positive to remove the |
A number or a string can be assigned additional properties, which converts them to Number or String objects. So in JavaScript, this is possible. const numberWithProps = Object.assign(5, {foo: "bar"}) and results in a But really, this is more of a typing issue. It doesn't sound reasonable to me to remove a feature that might or might not be used by who-knows-how-many of the 13k redux-related packages currently on npm. |
Nice, didn't realize you could do that. As you said, this is a typing issue. In reality, an enhancer can do whatever it wants to a store. It could change the state of shape completely (not just extend the state), which is something our types don't support. With any other type in Redux, I know of a package that uses that type. However, I have not seen a real-life usage of It seems odd to support a feature that considerably complicates our types that "who-knows-how-many of the 13k redux-related packages currently on npm" are relying on if that number could reasonably be 0. I'm not too worried about constraining the type to be just object-like, my main push for this change would be to simplify the types. |
My original concern of enhancers extending state not working for all reducers has been addressed. Thanks! |
Prior Issues
#1648
What is the current behavior?
The type for enhancers imply that enhancers should be allowed to extend the state. This is problematic for simple states that are just a number or a string because there is no way to extend them while still maintaining the original string or number. If you spread a string or a number, you'll get an empty object back, so if an enhancer tries to extend state, they'll likely end up erasing the user's state.
Steps to Reproduce
https://codesandbox.io/s/unruffled-faraday-u502r?fontsize=14&hidenavigation=1&theme=dark
What is the expected behavior?
The types should not allow you to create an enhancer that tries to extend the state because it's not possible to do so if the state is a string or number.
Environment Details
N/A
The text was updated successfully, but these errors were encountered: