Skip to content

Commit 1e5ec3e

Browse files
Merge pull request #215 from splitio/revert_hocs_and_render_props
Revert HOCs and Render Props components
2 parents b9cbb48 + 3e12176 commit 1e5ec3e

30 files changed

+1088
-675
lines changed

CHANGES.txt

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1-
2.0.0 (November XX, 2024)
1+
2.0.0 (November 1, 2024)
22
- Added support for targeting rules based on large segments.
33
- Added support for passing factory instances to the `factory` prop of the `SplitFactoryProvider` component from other SDK packages that extends the `SplitIO.IBrowserSDK` interface, such as `@splitsoftware/splitio-react-native`, `@splitsoftware/splitio-browserjs` and `@splitsoftware/browser-suite` packages.
44
- Updated @splitsoftware/splitio package to version 11.0.0 that includes major updates, and updated some transitive dependencies for vulnerability fixes.
55
- Renamed distribution folders from `/lib` to `/cjs` for CommonJS build, and `/es` to `/esm` for ECMAScript Modules build.
66
- Bugfixing - When the `config` prop is provided, the `SplitFactoryProvider` now makes the SDK factory and client instances available in the context immediately during the initial render, instead of waiting for the first SDK event (Related to https://github.com/splitio/react-client/issues/198). This change fixes a bug in the `useTrack` hook, which was not retrieving the client's `track` method during the initial render.
77
- BREAKING CHANGES:
8+
- Updated the default value of the `updateOnSdkUpdate` and `updateOnSdkTimedout` parameters of the `useSplitClient` and `useSplitTreatments` hooks options object to `true`, to re-render on all SDK events by default. The same applies for the equivalent props in the `[with]SplitClient` and `[with]SplitTreatments` components.
89
- Updated error handling: using the library modules without wrapping them in a `SplitFactoryProvider` component will now throw an error instead of logging it, as the modules requires the `SplitContext` to work properly.
9-
- Removed the `core.trafficType` configuration option and the `trafficType` parameter from the SDK `client()` method, `useSplitClient`, `useTrack`, and `SplitClient` component. This is because traffic types can no longer be bound to SDK clients in JavaScript SDK v11.0.0, and so the traffic type must be provided as first argument in the `track` method calls.
10-
- Removed deprecated modules: `SplitFactory` component, `useClient`, `useTreatments` and `useManager` hooks, and `withSplitFactory`, `withSplitClient` and `withSplitTreatments` high-order components. Refer to ./MIGRATION-GUIDE.md for instructions on how to migrate to the new alternatives.
11-
- Renamed some TypeScript interfaces: `ISplitFactoryProps` to `ISplitFactoryProviderProps`, and `ISplitFactoryChildProps` to `ISplitFactoryProviderChildProps`.
10+
- Updated the `SplitFactoryProvider` component to not accept a child as a function (render prop), to avoid unnecessary re-renders when using the library hooks. Refer to ./MIGRATION-GUIDE.md for instructions on how to migrate the child as a function to a regular component.
11+
- Removed the `core.trafficType` option from the SDK configuration object, and the `trafficType` parameter from the SDK `client()` method, `useSplitClient`, `useTrack`, `withSplitClient` and `SplitClient` component. This is because traffic types can no longer be bound to SDK clients in JavaScript SDK v11.0.0, and so the traffic type must be provided as first argument in the `track` method calls.
12+
- Removed deprecated modules: `SplitFactory` component, `useClient`, `useTreatments` and `useManager` hooks. Refer to ./MIGRATION-GUIDE.md for instructions on how to migrate to the new alternatives.
1213
- Renamed `SplitSdk` to `SplitFactory` function, which is the underlying Split SDK factory, i.e., `import { SplitFactory } from '@splitsoftware/splitio'`.
14+
- Renamed TypeScript interface: `ISplitFactoryProps` to `ISplitFactoryProviderProps`.
1315
- Dropped support for React below 16.8.0, as the library components where rewritten using the React Hooks API available in React v16.8.0 and above. This refactor simplifies code maintenance and reduces bundle size.
1416

1517
1.13.0 (September 6, 2024)

MIGRATION-GUIDE.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Notable changes to consider when migrating:
1414

1515
- When using the `config` prop with `SplitFactoryProvider`, the `factory` and `client` properties in `SplitContext` and the `manager` property in `useSplitManager` results are `null` in the first render, until the context is updated when some event is emitted on the SDK main client (ready, ready from cache, timeout, or update, depending on the configuration of the `updateOn<Event>` props of the component). This differs from the previous behavior where `factory`, `client`, and `manager` were immediately available. Nonetheless, it is not recommended to use the `client` and `factory` properties directly as better alternatives are available. For example, use the `useTrack` and `useSplitTreatments` hooks rather than the client's `track` and `getTreatments` methods.
1616

17-
- Updating the `config` prop in `SplitFactoryProvider` reinitializes the SDK with the new configuration, while `SplitFactory` does not reinitialize the SDK. You should pass a reference to the configuration object (e.g., via a global variable, `useState`, or `useMemo`) rather than a new instance on each render, to avoid unnecessary reinitializations.
17+
- Updating the `config` prop in `SplitFactoryProvider` re-initializes the SDK with the new configuration, while `SplitFactory` does not reinitialize the SDK. You should pass a reference to the configuration object (e.g., via a global variable, `useState`, or `useMemo`) rather than a new instance on each render, to avoid unnecessary re-initializations.
1818

1919
- Updating the `factory` prop in `SplitFactoryProvider` replaces the current SDK instance, unlike `SplitFactory` where it is ignored.
2020

package-lock.json

+17-17
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@splitsoftware/splitio-react",
3-
"version": "2.0.0-rc.2",
3+
"version": "2.0.0",
44
"description": "A React library to easily integrate and use Split JS SDK",
55
"main": "cjs/index.js",
66
"module": "esm/index.js",
@@ -63,7 +63,7 @@
6363
},
6464
"homepage": "https://github.com/splitio/react-client#readme",
6565
"dependencies": {
66-
"@splitsoftware/splitio": "11.0.0-rc.5",
66+
"@splitsoftware/splitio": "11.0.0",
6767
"memoize-one": "^5.1.1",
6868
"shallowequal": "^1.1.0",
6969
"tslib": "^2.3.1"

src/SplitClient.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { useSplitClient } from './useSplitClient';
1010
* The underlying SDK client can be changed during the component lifecycle
1111
* if the component is updated with a different splitKey prop.
1212
*
13-
* @see {@link https://help.split.io/hc/en-us/articles/360020448791-JavaScript-SDK#advanced-instantiate-multiple-sdk-clients}
13+
* @deprecated `SplitClient` will be removed in a future major release. We recommend replacing it with the `useSplitClient` hook.
1414
*/
1515
export function SplitClient(props: ISplitClientProps) {
1616
const { children } = props;

src/SplitContext.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@ import { ISplitContextValues } from './types';
33
import { EXCEPTION_NO_SFP } from './constants';
44

55
/**
6-
* Split Context is the React Context instance that represents our SplitIO global state.
7-
* It contains Split SDK objects, such as a factory instance, a client and its status (isReady, isTimedout, lastUpdate)
8-
* The context is created with default empty values, that SplitFactoryProvider and SplitClient access and update.
6+
* Split Context is the React Context instance provided by the SplitFactoryProvider and consumed by the Split Hooks.
7+
* It is used to share the SDK factory instance and other values across the application.
98
*/
109
export const SplitContext = React.createContext<ISplitContextValues | undefined>(undefined);
1110

src/SplitFactoryProvider.tsx

+12-14
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
11
import React from 'react';
22

3-
import { SplitClient } from './SplitClient';
43
import { ISplitFactoryProviderProps } from './types';
54
import { WARN_SF_CONFIG_AND_FACTORY } from './constants';
6-
import { getSplitFactory, destroySplitFactory, getSplitClient, getStatus } from './utils';
5+
import { getSplitFactory, destroySplitFactory, getSplitClient, getStatus, initAttributes } from './utils';
76
import { SplitContext } from './SplitContext';
87

98
/**
10-
* SplitFactoryProvider will initialize the Split SDK and its main client when `config` prop is provided or updated, listen for its events in order to update the Split Context,
11-
* and automatically destroy the SDK (shutdown and release resources) when it is unmounted or `config` prop updated. SplitFactoryProvider must wrap other library components and
12-
* functions since they access the Split Context and its properties (factory, client, isReady, etc).
9+
* The SplitFactoryProvider is the top level component that provides the Split SDK factory to all child components via the Split Context.
10+
* It accepts either an SDK `factory` instance or a `config` object as props to initialize a new SDK factory.
1311
*
1412
* NOTE: Either pass a `factory` instance or a `config` object as props. If both props are passed, the `config` prop will be ignored.
15-
* Pass the same reference to the `config` or `factory` object rather than a new instance on each render, to avoid unnecessary props changes and SDK reinitializations.
13+
* Pass the same reference to the `config` or `factory` object rather than a new instance on each render, to avoid unnecessary props changes and SDK re-initializations.
1614
*
1715
* @see {@link https://help.split.io/hc/en-us/articles/360038825091-React-SDK#2-instantiate-the-sdk-and-create-a-new-split-client}
1816
*/
1917
export function SplitFactoryProvider(props: ISplitFactoryProviderProps) {
20-
const { config, factory: propFactory } = props;
18+
const { config, factory: propFactory, attributes } = props;
2119

2220
const factory = React.useMemo(() => {
23-
return propFactory || (config ? getSplitFactory(config) : undefined);
24-
}, [config, propFactory]);
21+
const factory = propFactory || (config ? getSplitFactory(config) : undefined);
22+
initAttributes(factory && factory.client(), attributes);
23+
return factory;
24+
}, [config, propFactory, attributes]);
2525
const client = factory ? getSplitClient(factory) : undefined;
2626

27-
// Effect to initialize and destroy the factory
27+
// Effect to initialize and destroy the factory when config is provided
2828
React.useEffect(() => {
2929
if (propFactory) {
3030
if (config) console.log(WARN_SF_CONFIG_AND_FACTORY);
@@ -42,10 +42,8 @@ export function SplitFactoryProvider(props: ISplitFactoryProviderProps) {
4242
}, [config, propFactory]);
4343

4444
return (
45-
<SplitContext.Provider value={{
46-
factory, client, ...getStatus(client)
47-
}} >
48-
<SplitClient {...props} />
45+
<SplitContext.Provider value={{ factory, client, ...getStatus(client) }} >
46+
{props.children}
4947
</SplitContext.Provider>
5048
);
5149
}

src/SplitTreatments.tsx

+2-3
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,11 @@ import { useSplitTreatments } from './useSplitTreatments';
99
* call the 'client.getTreatmentsWithConfig()' method if the `names` prop is provided, or the 'client.getTreatmentsWithConfigByFlagSets()' method
1010
* if the `flagSets` prop is provided. It then passes the resulting treatments to a child component as a function.
1111
*
12-
* @see {@link https://help.split.io/hc/en-us/articles/360020448791-JavaScript-SDK#get-treatments-with-configurations}
12+
* @deprecated `SplitTreatments` will be removed in a future major release. We recommend replacing it with the `useSplitTreatments` hook.
1313
*/
1414
export function SplitTreatments(props: ISplitTreatmentsProps) {
1515
const { children } = props;
16-
// SplitTreatments doesn't update on SDK events, since it is inside SplitFactory and/or SplitClient.
17-
const context = useSplitTreatments({ ...props, updateOnSdkReady: false, updateOnSdkReadyFromCache: false });
16+
const context = useSplitTreatments(props);
1817

1918
return (
2019
<SplitContext.Provider value={context}>

0 commit comments

Comments
 (0)