launchdarkly-react-client-sdk
Version:
LaunchDarkly SDK for React
55 lines (54 loc) • 3.18 kB
TypeScript
import * as React from 'react';
import { LDClient } from 'launchdarkly-js-client-sdk';
import { ProviderConfig } from './types';
import { LDContext as HocState } from './context';
/**
* `withLDProvider` is a function which accepts a config object which is used to
* initialize `launchdarkly-js-client-sdk`.
*
* This HOC does three things:
* - It initializes the ldClient instance by calling `launchdarkly-js-client-sdk` initialize on `componentDidMount`
* - It saves all flags and the ldClient instance in the context API
* - It subscribes to flag changes and propagate them through the context API
*
* The difference between `withLDProvider` and `asyncWithLDProvider` is that `withLDProvider` initializes
* `launchdarkly-js-client-sdk` at `componentDidMount`. This means your flags and the ldClient are only available after
* your app has mounted. This can result in a flicker due to flag changes at startup time.
*
* `asyncWithLDProvider` initializes `launchdarkly-js-client-sdk` at the entry point of your app prior to render.
* This means that your flags and the ldClient are ready at the beginning of your app. This ensures your app does not
* flicker due to flag changes at startup time.
*
* @param config - The configuration used to initialize LaunchDarkly's JS SDK
* @return A function which accepts your root React component and returns a HOC
*/
export declare function withLDProvider(config: ProviderConfig): <P>(WrappedComponent: React.ComponentType<P>) => {
new (props: P): {
readonly state: Readonly<HocState>;
subscribeToChanges: (ldClient: LDClient) => void;
componentDidMount(): Promise<void>;
render(): JSX.Element;
context: any;
setState<K extends "flags" | "ldClient">(state: HocState | ((prevState: Readonly<HocState>, props: Readonly<P>) => HocState | Pick<HocState, K> | null) | Pick<HocState, K> | null, callback?: (() => void) | undefined): void;
forceUpdate(callback?: (() => void) | undefined): void;
readonly props: Readonly<P> & Readonly<{
children?: React.ReactNode;
}>;
refs: {
[key: string]: React.ReactInstance;
};
shouldComponentUpdate?(nextProps: Readonly<P>, nextState: Readonly<HocState>, nextContext: any): boolean;
componentWillUnmount?(): void;
componentDidCatch?(error: Error, errorInfo: React.ErrorInfo): void;
getSnapshotBeforeUpdate?(prevProps: Readonly<P>, prevState: Readonly<HocState>): any;
componentDidUpdate?(prevProps: Readonly<P>, prevState: Readonly<HocState>, snapshot?: any): void;
componentWillMount?(): void;
UNSAFE_componentWillMount?(): void;
componentWillReceiveProps?(nextProps: Readonly<P>, nextContext: any): void;
UNSAFE_componentWillReceiveProps?(nextProps: Readonly<P>, nextContext: any): void;
componentWillUpdate?(nextProps: Readonly<P>, nextState: Readonly<HocState>, nextContext: any): void;
UNSAFE_componentWillUpdate?(nextProps: Readonly<P>, nextState: Readonly<HocState>, nextContext: any): void;
};
contextType?: React.Context<any> | undefined;
};
export default withLDProvider;