-
Notifications
You must be signed in to change notification settings - Fork 13
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
Typical usage pattern not clear #42
Comments
Hi, |
So, in theory, according to react guides about hooks - i should be able to encompass my components in nested providers, like: const SignalRContext = createSignalRContext();
const SignalRSecondContext = createSignalRContext();
export const MainComponent = () => {
return (
<SignalRContext.Provider
url={'https://localhost:5000/hub'}
onOpen={con => console.log('Connection Id: ' + con.connectionId)}
onReconnect={con => console.log('Reconnected Id: ' + con.connectionId)}
>
<SignalRSecondContext.Provider
url={'https://localhost:10000/hub2'}
>
<ACustomComponent>
<NestedComponent />
</ACustomComponent>
</SignalRSecondContext.Provider>
</SignalRContext.Provider>
);
}; And be able to resolve the hook inside nested component for each one separately, yes? Possibly using some sort of a key, given that the constructor for provider would require said key, like Is this something that you could consider in the future or should I stay with my current approach stick with export This seems a bit dangerous, in theory you could import this anywhere, not just inside components encompassed by your provider(?). Also, thank you for such a quick reply! |
This issue is about react Context Provider, I don't think we should be wary about it.
I have no resistance to change, but I don't see any reason to change it now. Stay with it, i will fix any issue if you encounter it.
Creating an instance makes it easier to manage stuffs related to that instance. For example, we could make a different provider with specific parameters. |
Hi!
I really like your work, but I'm having trouble understanding the typical usage pattern - specifically "how should this work?" based on the README and examples.
Typically, I expect a custom context provider to work something like this (based on your library, in pseudo-code):
My assumption is that I should create a single provider for a large portion of my project, maintaining one connection throughout the user session. This would allow all components that need server communication to use your hooks/methods. In my case, SignalR will be used for:
Then, inside
ACustomComponent
and/orNestedComponent
(separate files for each component), I should be able to access the context's methods, specificallyinvoke
anduseSignalREffect
.However, from what I observe (and I may be wrong since I'm not deeply experienced with React), to access these functionalities, I have to create a new
SignalRContext
inside the aforementioned components. This causes the connection to benull
, and the functionality doesn't work.I've tried working around this by replacing:
with:
and using this export to access
invoke
anduseSignalREffect
.However, this seems incorrect as it contradicts the typical React context pattern usage which should allow me to just import "context" functionalities from your implementation and use them directly.
Am I doing/understanding something wrong way, does your implementation need adjustment or maybe there is no way around it?
The text was updated successfully, but these errors were encountered: