user14217
user14217

Reputation: 23

React context api - share data to with other packages

I have a react-native app (say, MyMobile) and I am also using a react-native library that has some shared components (say, MySharedUX). Both the folders are referring to same react-native versions.

package.json (MyMobile){
...
"@MySharedUX" : "file:../MySharedUX"
...

In the MySharedUX project I have created a pageTracking hook, which uses navigation context. When i invoke the hook from a function-component in MyMobile project, I get the navigation object as undefined.

export function usePageTracking(pageName: string): void {
    ...
    const navigation = useContext(NavigationContext);  //navigation is undefined.
    // const navigation = useNavigation();  // navigation is undefined.
    ...

But if I move the file usePageTracking inside MyMobile folder, the navigation get populated without any problem.

Though the package MySharedUX is executed within the MyMobile folder, it doesn't receive the context data.

Is there any way that MySharedUX can access the navigation context, without being passed-in as an argument to usePageTracking ?

Upvotes: 1

Views: 1265

Answers (1)

Raghav
Raghav

Reputation: 3058

AFAIK, Context APIs' hooks & methods are subject to in-memory data within APP boundary. If the data needs to be passed on to other packages, or a package that is loaded within your app, you can simply pass the object and re-build a react context inside the package's code.

Upvotes: 1

Related Questions