RFC: initial version of globalState hook #124
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR aims to be the starting point of a broader discussion about providing a global state solution within the VM.
Integration of
GlobalStateContext
to manage global state within the application using the contextAPI.Motivation:
As applications grow in complexity, efficiently managing state and data flow becomes crucial. A common pain point in building such complex applications is the cumbersome task of passing props down from the root to deeply nested child components. This not only makes the code harder to maintain but also limits component reusability.
Furthermore, due to the intrinsic architecture of our widgets, creating a context within one widget and leveraging it in another is currently not feasible. This creates a disjoined experience for developers and can lead to unnecessary prop drilling or resorting to less efficient state management solutions.
This PR addresses these challenges by introducing a GlobalStateContext leveraging React's Context API. With this, developers can easily access global state anywhere in the component tree through the useGlobalState hook without the hassle of passing props down multiple levels. Moreover, the new offers flexibility in setup; it can be initialized within a specific widget or can encompass the root VM component in the gate.
Changes:
GlobalStateContext
to enable centralized state management.GlobalStateContext
to allow Gateways inject the GlobalState into the root widget.GlobalStateContext
to access and manage global state within widgets.GlobalStateProvider
and the newuseGlobalState
hook.Examples
Creating the context within a widget:
Screen.Recording.2023-09-20.at.4.07.33.PM.mov
Injecting the context from the gateway:
Screen.Recording.2023-09-20.at.6.00.56.PM.mov