Skip to content

Commit

Permalink
Revert HOCs and Render Props components
Browse files Browse the repository at this point in the history
  • Loading branch information
EmilianoSanchez committed Nov 1, 2024
1 parent cdd1396 commit a715ff4
Show file tree
Hide file tree
Showing 20 changed files with 1,597 additions and 37 deletions.
28 changes: 28 additions & 0 deletions src/SplitClient.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import React from 'react';
import { SplitContext } from './SplitContext';
import { ISplitClientProps } from './types';
import { useSplitClient } from './useSplitClient';

/**
* SplitClient will initialize a new SDK client and listen for its events in order to update the Split Context.
* Children components will have access to the new client when accessing Split Context.
*
* The underlying SDK client can be changed during the component lifecycle
* if the component is updated with a different splitKey prop.
*
* @deprecated `SplitClient` will be removed in a future major release. We recommend replacing it with the `useSplitClient` hook.
*/
export function SplitClient(props: ISplitClientProps) {
const { children } = props;
const context = useSplitClient(props);

return (
<SplitContext.Provider value={context}>
{
typeof children === 'function' ?
children(context) :
children
}
</SplitContext.Provider>
)
}
5 changes: 3 additions & 2 deletions src/SplitFactoryProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';

import { ISplitFactoryProviderProps } from './types';
import { WARN_SF_CONFIG_AND_FACTORY } from './constants';
import { getSplitFactory, destroySplitFactory, initAttributes } from './utils';
import { getSplitFactory, destroySplitFactory, getSplitClient, getStatus, initAttributes } from './utils';
import { SplitContext } from './SplitContext';

/**
Expand All @@ -22,6 +22,7 @@ export function SplitFactoryProvider(props: ISplitFactoryProviderProps) {
initAttributes(factory && factory.client(), attributes);
return factory;
}, [config, propFactory, attributes]);
const client = factory ? getSplitClient(factory) : undefined;

// Effect to initialize and destroy the factory when config is provided
React.useEffect(() => {
Expand All @@ -41,7 +42,7 @@ export function SplitFactoryProvider(props: ISplitFactoryProviderProps) {
}, [config, propFactory]);

return (
<SplitContext.Provider value={{ factory }} >
<SplitContext.Provider value={{ factory, client, ...getStatus(client) }} >
{props.children}
</SplitContext.Provider>
);
Expand Down
25 changes: 25 additions & 0 deletions src/SplitTreatments.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import React from 'react';

import { SplitContext } from './SplitContext';
import { ISplitTreatmentsProps } from './types';
import { useSplitTreatments } from './useSplitTreatments';

/**
* SplitTreatments accepts a list of feature flag names and optional attributes. It accesses the client at SplitContext to
* call the 'client.getTreatmentsWithConfig()' method if the `names` prop is provided, or the 'client.getTreatmentsWithConfigByFlagSets()' method
* if the `flagSets` prop is provided. It then passes the resulting treatments to a child component as a function.
*
* @deprecated `SplitTreatments` will be removed in a future major release. We recommend replacing it with the `useSplitTreatments` hook.
*/
export function SplitTreatments(props: ISplitTreatmentsProps) {
const { children } = props;
const context = useSplitTreatments(props);

return (
<SplitContext.Provider value={context}>
{
children(context)
}
</SplitContext.Provider>
);
}
Loading

0 comments on commit a715ff4

Please sign in to comment.