You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//To share a React context between multiple npm packages, you can create a new package that exports the context provider and consumer components. Then, you can import and use these components in each of the other packages that need to access the shared context.
//Create a new package called "shared-context" that exports the context and its provider and consumer components.
//In each of the other packages that need to access the shared context, import the provider and consumer components from the "shared-context" package and use them in the appropriate components.
// package-a/index.js
import React from 'react';
import { SharedProvider } from 'shared-context';
function PackageA() {
return (
<SharedProvider value={someValue}>
{/* other components here */}
</SharedProvider>
);
}
// package-b/index.js
import React from 'react';
import { SharedConsumer } from 'shared-context';
function PackageB() {
return (
<SharedConsumer>
{value => /* render something with the shared value */}