0.2.6 • Published 11 days ago

@prefab-cloud/prefab-cloud-react v0.2.6

Weekly downloads
-
License
ISC
Repository
-
Last release
11 days ago

prefab-cloud-react

A React provider and hook for Prefab

Installation

npm install @prefab-cloud/prefab-cloud-react or yarn add @prefab-cloud/prefab-cloud-react

TypeScript types are included with the package.

Usage in your app

Configure the Provider

Wrap your component tree in the PrefabProvider, e.g.

import { PrefabProvider } from "@prefab-cloud/prefab-cloud-react";

const WrappedApp = () => {
  const context = {
    user: { email: "jeffrey@example.com" },
    subscription: { plan: "advanced" },
  };

  const onError = (error) => {
    console.error(error);
  };

  return (
    <PrefabProvider apiKey={"YOUR_API_KEY"} contextAttributes={context} onError={onError}>
      <App />
    </PrefabProvider>
  );
};

Here's an explanation of each provider prop:

propertyrequiredtypepurpose
apiKeyyesstringyour Prefab API key
onErrorno(error) => voidcallback invoked if prefab fails to initialize
contextAttributesnoContextAttributesthis is the context attributes object you passed when setting up the provider
endpointsnostring[]CDN endpoints to load configuration from (defaults to 2 prefab-based CDNs)
timeoutnonumberinitialization timeout (defaults to 10 seconds)
pollIntervalnonumberconfigures prefab to poll for updates every pollInterval ms.

Usage in Your Components

Use the usePrefab hook to fetch flags and config values:

const Logo = () => {
  const { isEnabled } = usePrefab();

  if (isEnabled("new-logo")) {
    return <img src={newLogo} className="App-logo" alt="logo" />;
  }

  return <img src={logo} className="App-logo" alt="logo" />;
};

usePrefab exposes the following:

const { isEnabled, get, loading, contextAttributes } = usePrefab();

Here's an explanation of each property:

propertyexamplepurpose
isEnabledisEnabled("new-logo")returns a boolean (default false) if a feature is enabled based on the current context
getget('retry-count')returns the value of a flag or config
loadingif (loading) { ... }a boolean indicating whether prefab content is being loaded
contextAttributesN/Athis is the context attributes object you passed when setting up the provider
prefabN/Athe underlying JavaScript prefab instance
keysN/Aan array of all the flag and config names in the current configuration

Usage in your test suite

Wrap the component under test in a PrefabTestProvider and provide a config object to set up your test state.

e.g. if you wanted to test the following trivial component

function MyComponent() {
  const { get, isEnabled, loading } = usePrefab();
  const greeting = get("greeting") || "Greetings";

  if (loading) {
    return <div>Loading...</div>;
  }

  return (
    <div>
      <h1 role="alert">{greeting}</h1>
      {isEnabled("secretFeature") && (
        <button type="submit" title="secret-feature">
          Secret feature
        </button>
      )}
    </div>
  );
}

You could do the following in jest/rtl

import { PrefabTestProvider } from '@prefab-cloud/prefab-cloud-react';

const renderInTestProvider = (config: {[key: string]: any}) => {
  render(
    <PrefabTestProvider config={config}>
      <MyComponent />
    </PrefabTestProvider>,
  );
};

it('shows a custom greeting', async () => {
  renderInTestProvider({ greeting: 'Hello' });

  const alert = screen.queryByRole('alert');
  expect(alert).toHaveTextContent('Hello');
});

it('shows the secret feature when it is enabled', async () => {
  renderInTestProvider({ secretFeature: true });

  const secretFeature = screen.queryByTitle('secret-feature');
  expect(secretFeature).toBeInTheDocument();
});

Contributing

Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated. For detailed contributing guidelines, please see CONTRIBUTING.md

0.2.6

11 days ago

0.2.5-rc1

14 days ago

0.2.5

14 days ago

0.2.4

18 days ago

0.2.3

1 month ago

0.2.2

4 months ago

0.2.1

4 months ago

0.1.21

5 months ago

0.2.0-rc1

5 months ago

0.2.0

5 months ago

0.1.20

7 months ago

0.1.11

11 months ago

0.1.12

11 months ago

0.1.13

10 months ago

0.1.14

10 months ago

0.1.15

10 months ago

0.1.16

9 months ago

0.1.17

8 months ago

0.1.18

7 months ago

0.1.19

7 months ago

0.1.10

11 months ago

0.1.8

11 months ago

0.1.7

1 year ago

0.1.4

1 year ago

0.1.6

1 year ago

0.1.5

1 year ago

0.1.3

2 years ago

0.1.2

2 years ago

0.0.1

2 years ago