Share global context that will be updated asynchronously from host to child app #1242
-
Hi everyone! I am using a third-party service So, I wrapped my Container/Host application with the authentication provider:
To access authentication data anywhere in the components tree the package provides us useAuth0 hook:
When the app is a first-time loaded, Container application makes the request to the external service to get the auth data and updates its context. Now I have an up-to-date authentication context inside my Container application. But now I am stuck on how can I get an up-to-date auth context inside my Micro-frontend app? Should I use some pub/sub system to notify my Micro frontend app about auth changes in the Container app? I've researched all the examples from the module-federation examples repository, but I didn't find a situation like this. I would appreciate any ideas. thanks in advance. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
I'm not 100% sure about this, but I think you can share the Context between the host and the child app by passing the Context instance that was created in the host: to the child app as a prop, and then re-declare the provider inside the child app like this: |
Beta Was this translation helpful? Give feedback.
-
Typically I use recoil for state distribution |
Beta Was this translation helpful? Give feedback.
I'm not 100% sure about this, but I think you can share the Context between the host and the child app by passing the Context instance that was created in the host:
const Auth0Context = React.createContext(defaultValue)
to the child app as a prop, and then re-declare the provider inside the child app like this:
<Auth0Context.Provider value={value}> ... </Auth0Context.Provider>