-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
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
[feature-request] Expose custom context to hooks #1304
Comments
What API are you envisioning for this? I think the factory approach would work, but it would be helpful to see some code. |
I've got a branch with the following as a proposal:
This is just one way we could expose it; my proposal branch is mostly clean code but there's still room for improvement. The key pieces are |
I think a 1:1 I'm not really groking |
Good point! I was trying to avoid making assumptions about the consumer's application, but ended up assuming all use cases would look like mine instead. 😅 I'll put up a PR within the next few days using the |
Out of curiosity, what's your use case for using a custom context instance rather than the default? |
Folks with multiple stores and an interleaved tree, I would assume. |
I think when i was trying to get tests to all pass on #1276 i ended up with each hook having a corresponding factory 1:1 |
@markerikson This would enable complex reusable components that can be published to npm. @timdorr hit the nail on the head. Consider a reusable () => (
<FriendList
renderAvatar={(userId) => <Avatar userId={userId} />}
/>
) Suppose |
I'm more asking what your specific use case is for multiple stores :) Why would |
In our case, we're going to publish our components, and they'll be used in apps which may or may not themselves use Redux. My goal is to make the component behave as if it were not using Redux at all -- I'd prefer to avoid telling consumers "install these 3 reducers, make sure In hindsight, I completely understand if this request doesn't fit with the goals of this project and isn't implemented as described 🙂 I'd be okay with factoring out and exposing |
I don't actually have a problem with the idea of a way to use a custom context with the hooks. After all, my initial API suggestion somewhere in the discussion threads was along the lines of I'm just trying to get a better understanding for where and why you feel you actually need the custom context yourself. I'm still not quite sure I follow the use case you're describing. Are you saying the components use a Redux store internally, and need to read from that as opposed to an app-wide store? |
Yes, that's exactly correct. Sorry for my lack of clarity 😅 I wouldn't mind |
We’ve been using react-redux with a custom store where parts of the tree have stateful needs that do not need shared with the whole app. In particular it’s useful where the tree might be repeated. We had a complex entity viewer with multiple nested accordion sections. It was helpful to rely on react’s useReducer and pass a custom store into react-redux to share that state with the viewer tree. Because the viewer component could be repeated, keeping that structure in the global redux store was more trouble than it was worth. |
What is the current behavior?
The
Provider
component allows consumers to pass a customcontext
, but the current hooks API does not allow that custom context to be used.What is the expected behavior?
I'd like to be able to access a custom context via React Redux hooks. I'd expect this would be implemented with a "hook factory" pattern for performance reasons, but that's an implementation detail.
Which versions of React, ReactDOM/React Native, Redux, and React Redux are you using? Which browser and OS are affected by this issue? Did this work in previous versions of React Redux?
This issue is unique to the
v7-hooks
branch. I suppose you could say this has never worked in previous versions of React Redux, since hook support is new 🙂or, it might be argued that the HOC pattern was able to access alternate contexts, so hook support would be incomplete without it.Note: I have a PR prepared; I didn't read the contributing guidelines until after I'd written the code, which is why I'm doubling back and creating the discussion issue now.
The text was updated successfully, but these errors were encountered: