-
Notifications
You must be signed in to change notification settings - Fork 208
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
(feat) Create an application-wide context for shared state #976
Conversation
9c39750
to
8b99d2c
Compare
Size Change: -383 kB (-9.3%) ✅ Total Size: 3.73 MB
ℹ️ View Unchanged
|
Cool idea, thanks for this. Right now it seems like we generally solve this kind of problem using hooks like |
So it's aimed at addressing of few things:
None of the 3 things I outlined there are specifically solved by this PR, but it's intended as a building block to solve them. Concretely this would be things like:
|
Love it. Those are great reasons. This PR seems like a good way to start; I look forward to seeing how this evolves. As a side note, when I was first designing the extension system, I imagined that extensions would receive props based on the context. So
!!! Wow I had no idea, that's quite bad |
0bf223f
to
b490ee8
Compare
Alright, I'm now happy this works as intended, albeit it's pretty minimally functional. The only current difference between this and a global store is that the value should only ever be a read-only copy of the value. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, @ibacher! Thanks.
Requirements
feat
,fix
, orchore
, among others). See existing PR titles for inspiration.For changes to apps
If applicable
Summary
This is currently a draft and more of a sketch of an idea than an actual implementation.
In essence, this actually ends up restoring something like the
AppState
that we used to have but did not use: a global state called the "context". The idea here is that contextually-important shared state can be pushed here and accessed from here. So, for example, the current session data could be stored and periodically refreshed or, for example, the current patient in a chart context (or current visit) could be stored here and hooks could defer to using it.There are a few differences between this iteration and the
AppState
:useAppContext()
basically returns read-only copies of the data. The idea here is that one component defines and maintains the context and other components can leverage it, but not update it.useDefineAppContextNamespace
and theOpenmrsAppContext
React component will clear the managed namespace from the context when the managing component is removed from the vDOM.The main usage of this from a client standpoint looks something like this, having one component that creates and provides the state and then any number of consumers.
In the provider, you might have something like:
And then in the consumer, something like:
Right now, this is pretty useless, but the goal is to enable some level of app-level caching beyond just relying on SWR de-duplicating requests.
Screenshots
Related Issue
Other
This setup may be too Redux-like to be a good idea in practice. While it doesn't explicitly require the use of reducers or state transitions, it does try to share a similar single point of responsibility for data updating, albeit in a much cruder way.