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

v1.0.5

Published

Modern A/B testing library with variant assignment and real-time config updates

Downloads

109

Readme

react-abkit

Modern A/B testing library with variant assignment and real-time config updates for React applications.

🚀 Features

  • TypeScript Support - Full type safety with TypeScript
  • React Hooks - Easy integration with React components
  • Deterministic Assignment - Consistent user experience across sessions
  • Local Storage - Persistent variant assignments
  • Lightweight - Minimal bundle size
  • Framework Agnostic Core - Use with any JavaScript framework
  • Custom Storage Adapters - Extend with your own storage solution

📦 Installation

npm install react-abkit

🔧 Quick Start

1. Basic Usage with React Hooks

Wrap app with provider

import React from "react";
import { ABTestProvider, useExperiment } from "react-abkit";

function App() {
  const abTest = new ABTest({
    logger: LokiLogger,
    flagAdapter: configCatAdapter,
  });

  return (
    <ABTestProvider abTest={abTest}>
      <MyMainComponent />
    </ABTestProvider>
  );
}

export const ABBoxColorCurrentUser: React.FC<Props> = ({ experimentKey }) => {
  const { variant } = useExperiment(experimentKey);

  if (variant === "blue") {
    return <ABBoxBlue />;
  }

  if (variant === "pink") {
    return <ABBoxPink />;
  }

  if (variant === "orange") {
    return <ABBoxOrange />;
  }
};
asdf

2. Direct Usage

import { ABTest, UserManager, LocalStorageAdapter } from "react-abkit";

// Initialize AB testing
const storage = new LocalStorageAdapter();
const userManager = new UserManager(storage);

// Initialize user
userManager.initializeUser(
  "user123",
  {
    email: "[email protected]",
    plan: "premium",
  },
  experimentConfigs
);

// Get variant for experiment
const variant = userManager.getVariant("pricing-test");
console.log(`User is in variant: ${variant}`);

📚 API Reference

Types

import type {
  UserData,
  ExperimentConfig,
  VariantType,
  ABTestConfig,
  Logger,
} from "react-abkit";

Core Classes

ABTest

Main class for A/B testing functionality.

UserManager

Manages user state and variant assignments.

// Initialize user
const user = userManager.initializeUser("user123", {
  email: "[email protected]",
  country: "US",
});

// Get assigned variant
const variant = userManager.getVariant("experiment-key");

// Update user attributes
// note: second option reassign variant for current user
userManager.updateUser(newUserData, true);

LocalStorageAdapter

Default storage adapter using browser's localStorage.

React Hooks

useExperiment(experimentKey: string)

Get variant assignment for an experiment.

const { variant, isVariant, isControl, isExperiment } =
  useExperiment("my-test");

useExperimentWithManager(userManager: UserManager, experimentKey: string)

Use experiment with a specific UserManager instance.

🏗️ Advanced Usage

Custom Storage Adapter

import { StorageAdapter } from "react-abkit";

class CustomStorageAdapter implements StorageAdapter {
  load(key: string): string | null {
    // Your custom load logic
    return customStorage.get(key);
  }

  save(key: string, value: string): void {
    // Your custom save logic
    customStorage.set(key, value);
  }

  remove(key: string): void {
    // Your custom remove logic
    customStorage.delete(key);
  }
}

With Logging

import { type Logger } from "react-abkit";

export const LokiLogger: Logger = {
  info: (msg, ctx) => sendToLoki({ level: "info", message: msg, ctx }),
  warn: (msg, ctx) => sendToLoki({ level: "warn", message: msg, ctx }),
  error: (msg, err, ctx) =>
    sendToLoki({
      level: "error",
      message: msg,
      error: err,
      ctx,
    }),
  debug: (msg, ctx) => sendToLoki({ level: "debug", message: msg, ctx }),
};

class ConfigCatAdapter implements FeatureFlagAdapter {
  private client: configcat.IConfigCatClient;

  constructor(sdkKey: string) {
    this.client = configcat.getClient(sdkKey);
  }

  async getFlag(key: string): Promise<boolean> {
    try {
      return await this.client.getValueAsync(key, false);
    } catch (error) {
      console.error("[ConfigCatAdapter] Failed to fetch flag:", key, error);
      return false;
    }
  }

  onFlagChange(key: string, callback: (value: boolean) => void): void {
    this.client.on("configChanged", async () => {
      const value = await this.getFlag(key);
      callback(value);
    });
  }
}

const abTest = new ABTest({
  logger: LokiLogger,
  flagAdapter: configCatAdapter,
});

With FeatureFlag

import { type FeatureFlagAdapter } from "react-abkit";

class ConfigCatAdapter implements FeatureFlagAdapter {
  private client: configcat.IConfigCatClient;

  constructor(sdkKey: string) {
    this.client = configcat.getClient(sdkKey);
  }

  async getFlag(key: string): Promise<boolean> {
    try {
      return await this.client.getValueAsync(key, false);
    } catch (error) {
      console.error("[ConfigCatAdapter] Failed to fetch flag:", key, error);
      return false;
    }
  }

  onFlagChange(key: string, callback: (value: boolean) => void): void {
    this.client.on("configChanged", async () => {
      const value = await this.getFlag(key);
      callback(value);
    });
  }
}

const abTest = new ABTest({
  flagAdapter: configCatAdapter,
});

🧪 Testing

The library includes comprehensive test coverage. Run tests with:

npm test

📄 License

MIT

🤝 Contributing

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

📞 Support


Made with ❤️ by Dinmukhammed