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 🙏

© 2025 – Pkg Stats / Ryan Hefner

react-settings-context

v0.0.5

Published

A [React](https://github.com/facebook/react) context that provides data storage for app settings that is backed by [cookies](https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies). This enables a simple storage mechanism that is compatible with SSR (s

Readme

React settings context

A React context that provides data storage for app settings that is backed by cookies. This enables a simple storage mechanism that is compatible with SSR (server-side rendering).

This library is most useful in the context of building demo or prototype apps with server side rendering.

View demo

Usage

  1. Install the helper in your Next.js app

    yarn add react-settings-context
  2. Use the context helper anywhere in your app

    import SettingsContext from 'react-settings-context'
    import React, { useContext } from 'react'
    export default function () {
      const [settings, patchSettings] = useContext(SettingsContext)
      return (
        <>
          <dl>
            <dt>is it funky?</dt>
            <dd>{settings.isItFunky ? 'yes' : 'no'}</dd>
          </dl>
          <button onClick={() => patchSettings({ isItFunky: settings.isItFunky! })}>
            turn {settings.isItFunky ? 'off' : 'on'} funky-ness
          </button>
        </>
      )
    }
  3. Modify the server side of you app so that the storage will be available on both client and server.

    Next.js helpers are provided and instructions are included below for integrating with other server-side rendering environments.

    Next.js

    Without existing _app.js

    If you don't already have a ./pages/_app.js in your app create one with the following content. This should be all you need and your app should start backing SettingsContext instances with cookies.

    import React from 'react'
    import App, { Container } from 'next/app'
    import SettingsContext, { getInitialPropsWithAppSettings } from 'react-settings-context'
    
    class MyApp extends App {
      render () {
        const { Component, pageProps, settings } = this.props
        return (
          <SettingsContext.Provider initialData={settings}>
            <Container>
              <Component {...pageProps} />
            </Container>
          </SettingsContext.Provider>
        )
      }
    }
    
    MyApp.getInitialProps = getInitialPropsWithAppSettings()
    export default MyApp
    With existing _app.js

    If you're not customizing your _app.js's getInitialProps you can probably use the previous example as a guide. Otherwise another helper is provided for using inside getInitialProps.

    // ...SNIP...
    
    MyApp.getInitialProps = async ({ Component, ctx }) {
      const settings = getAppSettingsFromCookie(ctx.req)
      let pageProps = {}
      if (Component.getInitialProps) {
        pageProps = await Component.getInitialProps(ctx)
      }
      return { pageProps, settings }
    }
    
    export default MyApp

    Other SSR scenarios

    Under the covers the above Next.js helpers do two things:

    1. Parse cookies from req.headers
    2. Wrap the app's root component in a React ContextProvider

    A helper is exposed for parsing the cookies. This can be used in request handlers and piped into your top level component via props.

    import { getAppSettingsFromCookie } from 'react-settings-context'
    const settings = getAppSettingsFromCookie('foo', req)

    And a Context.Provider is exposed that ties everything together. How the settings value from above is passed to the component is framework-depenent.

    import SettingsContext from 'react-settings-context'
    
    function App () {
      render () {
        return {
          <SettingsContext.Provider initialData={settings}>
            <YourPageComponent />
          </SettingsContext.Provider>
        }
      }
    }
  4. Configuring cookie (optional)

    Cookie reading/writing behavior and configuration can be controlled by passing an options object to either getInitialPropsWithAppSettings or getAppSettingsFromCookie. Reasonable defaults are in place and in many cases configuration won't be necessary. The options listed here are used as described and all other values will be passed to the underlying cookie parsing library.

    API

    getAppSettingsFromCookie/getInitialPropsWithAppSettings

    name

    The name of the cookie. Useful if you will run more than one app on thee same domain. Defaults to app-settings

    req

    The (incoming server-side request)[https://nodejs.org/api/http.html#http_class_http_incomingmessage]. This is used both to denote that the cookie is being read server-side and to expose the cookie headers to the library.

    Passed to cookie parsing library

    The following values supplied are by this library as defaults and passed to the cookie library are not passed in

    • maxAge: 2592000 (2 weeks in ms)
    • secure set to true if window location protocol is https
    • domain set to the domain found in current window location

Why not localStorage?

The main motivation of this library is to enable the rapid production of demo/prototype apps on the Next.js framework (SSR javascript). Using localStorage in this context will cause (a visible re-render)[https://localstorage-settings-demo.arthack.io] each time a page is rendered on the server and then continued on the client because the server will render with the default values irrespective of localStorage and then the client will have to re-render with the localStorage values.