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-switchboard

v0.0.27

Published

Quickly create custom DevTools for your React app

Downloads

245

Readme

React Switchboard 🎛

Quickly create custom DevTools for your React app.

Quick Start

npm install react-switchboard -D

Vite Example

Call Switchboard in your project root. Pass your app's main component to Switchboard's appSlot prop. Import Switchboard CSS. Lazy load Switchboard via an environment variable so it's excluded from your production bundle.

import { lazy } from "react";
import { createRoot } from "react-dom/client";
import "react-switchboard/dist/index.css";

const Switchboard = lazy(() => import("react-switchboard"));

createRoot(document.getElementById("root")!).render(
  import.meta.env.PROD ? (
    <App />
  ) : (
    <Suspense fallback="Loading Switchboard...">
      <Switchboard appSlot={<App />} />
    </Suspense>
  )
);

Headless

The Switchboard component accepts children so you can specify what it renders. If you want complete control over the UI, use the useSwitchboard and useSwitchboardState hooks instead of the Switchboard component.

function CustomSwitchboard() {
  const { generalSettings, switchboardWindowRef, copySettingsUrlToClipboard } =
    useSwitchboard();

  // Use useSwitchboardState hook for custom settings.
  const [user, setUser] = useSwitchboardState("sb-user", null);

  return {
    /* Your custom JSX to render your desired UI */
  };
}

Why Switchboard?

Code faster. Reproduce edge cases. Do real-time demos. Use Switchboard to configure automated tests.

Common Uses

  • Login / switch users instantly
  • Change feature toggles
  • Configure mock APIs
  • Force errors
  • Simulate network slowness for specific requests
  • Configure automated test scenarios
  • Simulate incoming traffic and write conflicts

More info in this 25 minute conference talk: Creating Custom Dev Tools for Your React App at React Rally

API

Components

  • Switchboard - The main component that renders your app and the Switchboard UI.

Hooks

  • useSwitchboard - Logic for running Switchboard. Useful to create a custom Switchboard UI.
  • useSwitchboardState - Declare Switchboard state. This state is automatically initialized from the URL, and written to localStorage so that it persists between sessions. Useful to extend Switchboard's features with custom settings for your app, or if you want to create a custom Switchboard UI.

FAQ

  • How does mocking work? Switchboard intercepts fetch requests via Mock Service Worker, and displays a UI for configuring the mock responses.

  • Why does Switchboard render my app? If you configure Switchboard to force the app to throw an error, Switchboard continues to render so you can change Switchboard's settings.

  • Why lazy loading? Lazy load Switchboard via React.lazy and Suspense so that it's excluded your app's prod bundle.

  • How can I toggle Switchboard? Use an environment variable to enable Switchboard. For example, tweak Vite's dev npm script to set an environment variable using cross-env:

"dev": "cross-env VITE_ENABLE_SWITCHBOARD=Y vite",

Then, read this environment variable in your app's entry point.

Acknowledgements

Inspiration