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

@4chain-ag/react-configuration

v1.0.4

Published

React runtime environment variables (env) configuration

Downloads

159

Readme

React-configuration

React-configuration is a solution for ever-existing problem with environment variables being injected into the application at build time. This requires rebuilding the application every time an env changes.

React-configuration allows for easy setup of runtime variables without any dependencies, server setups or injecting javascript into the window in index.html (which can be very dangerous in terms of security).

Instead, it uses a JSON file with a React ContextProvider to allow for runtime change and overriding of variables.

It works with typescript out of the box.

It also provides an easy solution for overriding environment variables in nginx image in docker see below.

Table of contents

  1. How to use it
  2. Advanced
  3. Troubleshooting

How to use it

Installation

npm install @4chain-ag/react-configuration

Usage

  1. Create a config.default.json file in the public/ directory. For example:
{
  "apiUrl": "localhost:8000",
  "otherEnvVariable": "value"
}
  1. In your index.js file import and add ConfigProvider:
import { ConfigProvider } from "@4chain-ag/react-configuration";

const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(
  <React.StrictMode>
    <ConfigProvider> {/* It will allow to use useConfig hook inside every child component of <App /> */}
      <App />
    </ConfigProvider>
  </React.StrictMode>
);

It doesn't have to be an index.js file, you can choose any other file, but config variables will only be available in child components. Read more about React Context

  1. Use useConfig hook anywhere you need env variables:
import { useConfig } from "@4chain-ag/react-configuration"

export const ShowConfigComponent = () => {
  const { config } = useConfig()

  return (
    <h3>The api url is: {config.apiUrl}</h3>
  )
}

Advanced

Overriding default config locally

In Vite

In order to override the config variables in vite, create an env-config.json file in the root directory of your project:

{
  "apiUrl": "localhost:3000/override/value"
}

You can override just one, many or all the variables.

In Create-React-App (CRA)

In order to override the config variables in CRA, you need to:

  1. Create a setupProxy.js file in the root of your project and write the following:
const config = require("@4chain-ag/react-configuration");
const express = require("express");

module.exports = config.expressProxy(express);
  1. Create an env-config.json file in the root directory of your project:
{
  "apiUrl": "localhost:3000/override/value"
}

Just as in Vite, you can override just one, many or all the variables.


Overriding in nginx image in docker

In order to override the config variables in nginx image in docker you need to:

  1. Create an env-config.json file the directory of your choice (doesn't need to be project root)
{
  "apiUrl": "localhost:3000/override/value/docker"
}
  1. Add a volume with this file to your docker configuration. Example in docker-compose.yml:
services:
  app:
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - '3000:3000'
    volumes:
      - '/path/to/env-config.json:/usr/share/nginx/html/env-config.json'

If the file is in the root folder of the project, it is enough to put just a file:

volumes:
  - 'env-config/json:/usr/share/nginx/html/env-config.json'

If the file is somewhere else, you need to specify path to file:

volumes:
  - '/path/to/env-config/json:/usr/share/nginx/html/env-config.json'

Using loadConfig without the ContextProvider

If you don't want to use the useConfig hook inside the ContextProvider, you can just use the async function for loading config from file.

import { loadConfigFromFile } from "@4chain-ag/react-configuration";

const config = await loadConfigFromFile()

This function will return the default config from config.default.json inside the public/ directory or a merged config, if a file env-config.json is specified in the root directory of the project.


Troubleshooting

Null values

Null values in javascript/typescript are treated as non-existent, so they are not supported as values that can be overriden. Therefore, avoid null values and try to set default or empty values instead.

{
  "apiUrl": null    // WRONG ❌
  "apiUrl": ""      // CORRECT ✅
}