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-client-settings

v1.1.0

Published

`react-client-settings` is a zero dependency library for React applications that provides a context provider and a hook to manage global application settings (variables) in the client. Common scenario is to expose environment variables in the backend to t

Downloads

58

Readme

react-client-settings

react-client-settings is a zero dependency library for React applications that provides a context provider and a hook to manage global application settings (variables) in the client. Common scenario is to expose environment variables in the backend to the client. The library retrieves the settings from any configurable backend and stores the result in a React context. The provided hook can then be used to access these settings throughout your application.

Features

  • Fetch settings from a backend API upon initialization.
  • Store settings in a React context for easy access.
  • Provide a hook for consuming settings within your components.

Installation

Install the package using npm, yarn or pnpm:

npm install react-client-settings

or

yarn add react-client-settings

or

pnpm add react-client-settings

Usage

Create typed context provider and hook.

// my-settings.ts
import { createSettings } from 'react-client-settings'

export type SettingsData = {
  showDevelopmentFeature: boolean
  numberOfAnything?: number
}

export const { SettingsProvider, useSettings } = createSettings<SettingsData>({
  uri: '/api/settings',
})

Note: Replace /api/settings with the actual endpoint from which your application fetches its settings. Ensure that the settings API responds with a JSON object that can be consumed by the context provider.

Context Provider

Wrap your application with the Settings Provider to make the settings available throughout your component tree.

Parameters to Settings Provider:

| Name | Description | Mandatory | |:---------|:-------------------------------------------------|-----------| | loading | Component to render when settings are loaded | No | | error | Component to render when fetch of settings fails | No |

import { SettingsProvider } from './my-settings';

// Next.js
export default function MyApp({ Component, pageProps }) {
  return (<SettingsProvider>
             <Component {...pageProps} />
          </SettingsProvider>
  );
}

Hook

Use the useSettings hook to access the settings within your components.

import { useSettings } from './my-settings';

const MyComponent = () => {
  const { showDevelopmentFeature } = useSettings();

  return (
    <div>
      <h1>Application Settings</h1>
      {showDevelopmentFeature && <p>Development feature</p>}
    </div>
  );
};

export default MyComponent;

Enjoy using react-client-settings! If you have any questions or feedback, feel free to open an issue on the GitHub repository.

Contributing

Contributions are welcome! Please open an issue or submit a pull request on GitHub.

License

This project is licensed under the MIT License.