npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-singleton-context

v1.0.5

Published

A small library designed to register and return singletonized React contexts

Downloads

10,484

Readme

react-singleton-context

About this project

React Singleton Context is a small library to alleviate the pain points that come with using React Context in micro-frontend architectures.

It is designed to be defined as a singleton, and it serves only one purpose: to offload the singleton requirements of React Context. This allows library maintainers to write their own modules, leverage context, and not have to worry about their own library being a singleton.

How do I know if I need it?

If you have all three aspects below, chances are you'll want to use React Singleton Context:

  • A host application / platform which employs Webpack's Module Federation.
  • A JavaScript / TypeScript library which employs React Context.
  • A federated application loaded by the above host application which employs the above library.

This is a rather niche architecture, but if you're here, likely you've run into a problem using React Context: no matter how you accessed your context in your federated application, it would not resolve until you singletonized the library.

But singletonization of the library, in solving one problem, creates another: the exact version of the singleton loaded by Webpack is defined by the entry point to your host application. If you have many entry points, and if those entry points are on different versions of the singletonized library, you'll eventually encounter version mismatch problems. And no one wants that!

So you're presented with two options:

  1. Keep all entry points constantly up-to-date with the latest version of the singletonized library.
  2. Use React Singleton Context.

Option 1 is operationally expensive. Option 2, we outline below.

How to use React Singleton Context

1. Install React Singleton Context into the host application

First, add the package as a dependency, like so:

npm install react-singleton-context

or, if using Yarn:

yarn add react-singleton-context

Once that's done, go ahead and add it as an eager singleton to your Webpack config's Module Federation plugin declaration:

	new ModuleFederationPlugin({
		...
		shared: {
			...
  			'react-singleton-context': { singleton: true, eager: true },
			...
		}

You'll also want to import the context registry into whatever bootstrapping code is always run by your host application upon client entry. That way, the entry point always loads the canonical version of the context registry, and it'll singletonize itself and any contexts created by it properly.

Something like this will suffice:

import 'react-singleton-context';

Remember, your host application's setup might differ slightly from the above, so refer to your host application's owning team for exact details, if necessary.

2. Install React Singleton Context into any build-time library dependencies federated applications use

Like you did with the host application, it's time to add the package to the library:

npm install react-singleton-context --save-dev

or, if using Yarn:

yarn add react-singleton-context --dev

Then, add react-singleton-context as a peerDependency for this library, too. Doing so will necessitate a major version bump for your library, as that's a breaking change.

The following is example usage that a library maintainer might employ in the actual application code.

import { createRegisteredContext } from 'react-singleton-context';

interface IExampleContext {
    message: string;
}

// Create the React.Context with createRegisteredContext instead of React.createContext
const ExampleContext = createRegisteredContext<IExampleContext | null>(
    'MyLibraryNameExampleContext', // a sufficiently globally unique displayName
    null // default value
);

// Export like normal; all consumers will be using a "singletonized" context
export default ExampleContext;

or, if not using TypeScript:

import { createRegisteredContext } from 'react-singleton-context';

// Create the React.Context with createRegisteredContext instead of React.createContext
const ExampleContext = createRegisteredContext(
    'MyLibraryNameExampleContext', // a sufficiently globally unique displayName
    null // default value
);

// Export like normal; all consumers will be using a "singletonized" context
export default ExampleContext;

3. Update the build-time library version in federated applications

Now that your platform and your library are ready, it's time to update the federated application's dependencies to leverage the latest and greatest.

Optionally, if your library was previously singletonized (because you weren't able to use React Singleton Context previously to get around the context problem), you can safely de-singletonize it in the same commit.

And that's it! You're now using singletonized context.