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

@shopify/react-universal-provider

v3.1.4

Published

Factory function and utilities to create self-serializing/deserializing providers that work for isomorphic applications

Downloads

44,719

Readme

@shopify/react-universal-provider

Build Status Build Status License: MIT npm version npm bundle size (minified + gzip)

Factory function and utilities to create self-serializing/deserializing providers that work for isomorphic applications.

Table of contents

  1. Installation
  2. What is a Universal Provider?
  3. Usage

Installation

yarn add @shopify/react-universal-provider

What is a Universal Provider ?

A universal provider is a React component that leverages the useSerialized() hook from @shopify/react-html to handle both serializing data during server rendering, and deserializing it on the client. The components often render React.context() providers to make their serialized state available to the rest of the app they're rendered in.

Comparison with traditional serialization techniques

Consider the I18nUniversalProvider. In the server you may want to set the locale based on the Accept-Language header. The client needs to be informed of this somehow to make sure React can render consistently. Traditionally you might do something like:

On the server

  • Get the locale from the Accept-Language header in your node server
  • Create an instance of I18nManager with that locale
  • Pass the instance into your app
  • Render a <Serialize name="locale" /> component in the DOM you send to the client

On the client

  • Call getSerialized('locale') in your client entry point for your locale
  • Create an instance of I18nManager with that locale
  • Pass the instance into your app

In your react app's universal code

  • Have your app render an <I18nProvider /> with the manager from props

With universal provider you would instead:

On the server

  • Get the locale from the Accept-Language header in your node server
  • Pass the locale into your app directly

On the client

  • Render your app without passing in the locale

In your react app's universal code

Since universal provider handle the details of serialization they allow you to remove code from your client/server entry points and instead let the react app itself handle those concerns.

Usage

This package provide a function which can be use to create a universal provider given a React Context object. It is particularly useful for simple object or primitive that need to be sync up between the server and client.

The list below show a few packages with more complex universal provider:

@shopify/react-app-bridge-universal-provider @shopify/react-csrf-universal-provider @shopify/react-graphql-universal-provider` @shopify/react-i18n-universal-provider

createUniversalProvider

Options

The function takes a unique id and a React Context object.

Props

The resulting Provider takes children and a data prop.

interface Props<Value> {
  value?: Value;
  children?: React.ReactNode;
}

Example

//ApiKeyUniversalProvider.tsx
import {createContext} from 'react';
import {createUniversalProvider} from '@shopify/react-universal-provider';

export const ApiKeyContext = createContext<string | null>(null);
export const ApiKeyUniversalProvider = createUniversalProvider(
  'api-key',
  ApiKeyContext,
);
// App.tsx
import {ApiKeyProvider} from './ApiKeyProvider';

function App({apiKey}: {apiKey?: string}) {
  return (
    <ApiKeyProvider value={apiKey}>{/* rest of the app */}</ApiKeyProvider>
  );
}