Skip to content

Commit 03c7bc9

Browse files
Merge pull request #188 from splitio/migration-guide
Add MIGRATION-GUIDE.md file
2 parents 5fa09ae + d8a77bf commit 03c7bc9

File tree

7 files changed

+185
-18
lines changed

7 files changed

+185
-18
lines changed

CHANGES.txt

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
1.11.0 (January 16, 2023)
2-
- Added the new `SplitFactoryProvider` component as a replacement for the now deprecated `SplitFactory` component.
3-
The new component is a revised version of `SplitFactory`, addressing improper handling of the SDK initialization side-effects in the `componentDidMount` and `componentDidUpdate` methods (commit phase), causing some issues like memory leaks and the SDK not reinitializing when component props change (Related to issue #11 and #148).
4-
The `SplitFactoryProvider` component can be used as a drop-in replacement for `SplitFactory`. It utilizes the React Hooks API, that requires React 16.8.0 or later, and supports server-side rendering. See our documentation for more details (Related to issue #11 and #109).
2+
- Added new `SplitFactoryProvider` component as a replacement for the now deprecated `SplitFactory` component.
3+
- Bugfixing: The new component is a revised version of `SplitFactory` that properly handles SDK side effects (i.e., factory creation and destruction) within the React component lifecycle,
4+
- resolving memory leak issues in React development mode, strict mode and server-side rendering (Related to issues #11 and #109),
5+
- and also ensuring that the SDK is updated if `config` or `factory` props change (Related to issues #11 and #148).
6+
- Notable changes when migrating from `SplitFactory` to `SplitFactoryProvider`:
7+
- `SplitFactoryProvider` utilizes the React Hooks API, requiring React 16.8.0 or later, while `SplitFactory` is compatible with React 16.3.0 or later.
8+
- 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.
9+
- 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.
10+
- Updating the `factory` prop in `SplitFactoryProvider` replaces the current SDK instance, unlike `SplitFactory` where it is ignored.
511
- Updated internal code to remove a circular dependency and avoid warning messages with tools like PNPM (Related to issue #176).
612
- Updated @splitsoftware/splitio package to version 10.25.1 for vulnerability fixes.
713

MIGRATION-GUIDE.md

+142
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
2+
# Migrating to get React SDK v1.11.0 improvements: Replacing the deprecated `SplitFactory` and `withSplitFactory` components
3+
4+
Starting from React SDK v1.11.0, the `SplitFactoryProvider` component is available and can replace the older `SplitFactory` and `withSplitFactory` components. The deprecated components will continue working, until they are removed in a future major release.
5+
6+
We recommend migrating to the new `SplitFactoryProvider` component instead. This component is a revised version of `SplitFactory` that properly handles SDK side effects (i.e., factory creation and destruction) within the React component lifecycle. By migrating, you can benefit from a number of improvements:
7+
8+
- Resolution of memory leak issues in React development mode, strict mode, and server-side rendering.
9+
10+
- Updating the SDK when `config` or `factory` props change.
11+
12+
Notable changes to consider when migrating:
13+
- `SplitFactoryProvider` utilizes the React Hooks API, requiring React 16.8.0 or later, while `SplitFactory` is compatible with React 16.3.0 or later.
14+
15+
- 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.
16+
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.
18+
19+
- Updating the `factory` prop in `SplitFactoryProvider` replaces the current SDK instance, unlike `SplitFactory` where it is ignored.
20+
21+
To migrate your existing code, replace:
22+
23+
```javascript
24+
const MyApp = () => {
25+
return (
26+
<SplitFactory config={mySplitConfig}>
27+
<MyComponent />
28+
</SplitFactory>
29+
);
30+
};
31+
```
32+
33+
or
34+
35+
```javascript
36+
const MyApp = withSplitFactory(mySplitConfig)(MyComponent);
37+
```
38+
39+
with:
40+
41+
```javascript
42+
const MyApp = () => {
43+
return (
44+
<SplitFactoryProvider config={mySplitConfig}>
45+
<MyComponent />
46+
</SplitFactoryProvider>
47+
);
48+
};
49+
```
50+
51+
and consider that `factory`, `client` and `manager` properties might be `null` until the SDK has emitted some event:
52+
53+
```javascript
54+
const MyComponent = () => {
55+
// factoryFromContext === factory, clientFromContext === client, and they are null until some SDK event is emitted
56+
const { factory: factoryFromContext, client: clientFromContext } = useContext(SplitContext);
57+
const { factory, client } = useSplitClient();
58+
59+
// Example to evaluate all your flags when the SDK is ready and re-evaluate on SDK_UPDATE events
60+
const { manager } = useSplitManager();
61+
const FEATURE_FLAG_NAMES = manager ? manager.names() : [];
62+
const { treatments, isReady } = useSplitTreatments({ names: FEATURE_FLAG_NAMES, updateOnSdkUpdate: true }); // updateOnSdkReady is true by default
63+
64+
return isReady ?
65+
treatments['feature-flag-1'].treatment === 'on' ?
66+
<FeatureOn /> :
67+
<FeatureOff /> :
68+
<LoadingPage />
69+
}
70+
```
71+
72+
# Migrating to get React SDK v1.10.0 improvements: Replacing the deprecated `useClient`, `useTreatments`, and `useManager` hooks
73+
74+
Starting from React SDK v1.10.0, the `useSplitClient`, `useSplitTreatments`, and `useSplitManager` hooks are available and can replace the older `useClient`, `useTreatments`, and `useManager` hooks respectively. The deprecated hooks will continue working, until they are removed in a future major release.
75+
76+
We recommend migrating to the new versions `useSplitClient`, `useSplitTreatments` and `useSplitManager` respectively, which provide a more flexible API:
77+
78+
- They accept an options object as parameter, instead of a list of parameters as their deprecated counterparts. The options object can contain the same parameters as the old hooks, plus some extra optional parameters: `updateOnSdkReady`, `updateOnSdkReadyFromCache`, `updateOnSdkTimedout`, and `updateOnSdkUpdate`, which control when the hook updates the component. For example, you can set `updateOnSdkUpdate` to `true`, which is `false` by default, to update the component when an `SDK_UPDATE` event is emitted. This is useful when you want to avoid unnecessary re-renders of your components.
79+
80+
- They return an object containing the SDK status properties. These properties are described in the ['Subscribe to events and changes' section](https://help.split.io/hc/en-us/articles/360038825091-React-SDK#subscribe-to-events-and-changes) and enable conditional rendering of components based on the SDK status, eliminating the need to access the Split context or use the client's `ready` promise or event listeners. For example, you can show a loading spinner until the SDK is ready, and use the `treatments` result to render the variants of your app once the SDK is ready.
81+
82+
The usage of the new hooks is shown below:
83+
84+
```js
85+
const { client, isReady, isReadyFromCache, hasTimedout, lastUpdate } = useSplitClient({ splitKey: userId, updateOnSdkUpdate: true });
86+
const { treatments, isReady, isReadyFromCache, hasTimedout, lastUpdate } = useSplitTreatments({ names: ['feature-flag-1'], updateOnSdkTimedout: false });
87+
const { manager, isReady, isReadyFromCache, hasTimedout, lastUpdate } = useSplitManager();
88+
```
89+
90+
91+
92+
To migrate your existing code, replace:
93+
94+
```javascript
95+
const client = useClient(optionalSplitKey, optionalTrafficType, optionalAttributes);
96+
const treatments = useTreatments(featureFlagNames, optionalAttributes, optionalSplitKey);
97+
const manager = useManager();
98+
```
99+
100+
with:
101+
102+
```javascript
103+
const { client } = useSplitClient({ splitKey: optionalSplitKey, trafficType: optionalTrafficType, attributes: optionalAttributes });
104+
const { treatments } = useSplitTreatments({ names: featureFlagNames, attributes: optionalAttributes, splitKey: optionalSplitKey });
105+
const { manager } = useSplitManager();
106+
```
107+
108+
and use the status properties to conditionally render your components. For example, use the following code:
109+
110+
```javascript
111+
const MyComponent = ({ userId }) => {
112+
113+
const { treatments, isReady } = useSplitTreatments({ names: [FEATURE_X], splitKey: userId })
114+
115+
return isReady ?
116+
treatments[FEATURE_X].treatment === 'on' ?
117+
<FeatureOn /> :
118+
<FeatureOff /> :
119+
<LoadingPage />
120+
}
121+
```
122+
123+
instead of:
124+
125+
```javascript
126+
const MyComponent = ({ userId }) => {
127+
128+
const [sdkIsReady, setSdkIsReady] = useState(false);
129+
const client = useClient(userId);
130+
const treatments = useTreatments([FEATURE_X], undefined, userId);
131+
132+
useEffect(() => {
133+
if (client) client.ready().then(() => setSdkIsReady(true));
134+
}, [client]);
135+
136+
return isReady ?
137+
treatments[FEATURE_X].treatment === 'on' ?
138+
<FeatureOn /> :
139+
<FeatureOff /> :
140+
<LoadingPage />
141+
}
142+
```

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"files": [
99
"README.md",
1010
"CONTRIBUTORS-GUIDE.md",
11+
"MIGRATION-GUIDE.md",
1112
"LICENSE",
1213
"CHANGES.txt",
1314
"src",

src/SplitFactory.tsx

+13-4
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,19 @@ import { DEFAULT_UPDATE_OPTIONS } from './useSplitClient';
1414
* The underlying SDK factory and client is set on the constructor, and cannot be changed during the component lifecycle,
1515
* even if the component is updated with a different config or factory prop.
1616
*
17-
* @deprecated Replace with the new `SplitFactoryProvider` component.
18-
* `SplitFactoryProvider` is a drop-in replacement that properly handles side effects (factory creation and destruction) within the React component lifecycle, avoiding issues with factory recreation and memory leaks.
19-
* Note: There is a subtle breaking change in `SplitFactoryProvider`. When using the `config` prop, `factory` and `client` properties in the context are `null` in the first render, until the context is updated when some event is emitted on
20-
* the SDK main client (ready, ready from cache, timeout or update depending on the configuration of the `updateOnXXX` props of the component). This differs from the previous behavior where `factory` and `client` were immediately available.
17+
* @deprecated `SplitFactory` will be removed in a future major release. We recommend replacing it with the new `SplitFactoryProvider` component.
18+
*
19+
* `SplitFactoryProvider` is a revised version of `SplitFactory` that properly handles SDK side effects (i.e., factory creation and destruction) within the React component lifecycle,
20+
* resolving memory leak issues in React development mode, strict mode and server-side rendering, and also ensuring that the SDK is updated if `config` or `factory` props change.
21+
*
22+
* Notable changes to consider when migrating:
23+
* - `SplitFactoryProvider` utilizes the React Hooks API, requiring React 16.8.0 or later, while `SplitFactory` is compatible with React 16.3.0 or later.
24+
* - When using the `config` prop with `SplitFactoryProvider`, the `factory` and `client` properties in `SplitContext` and the `manager` property in `useSplitManager` results
25+
* 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
26+
* the configuration of the `updateOn<Event>` props of the component). This differs from the previous behavior where `factory`, `client`, and `manager` were immediately available.
27+
* - Updating the `config` prop in `SplitFactoryProvider` reinitializes the SDK with the new configuration, while `SplitFactory` does not reinitialize the SDK. You should pass a
28+
* 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.
29+
* - Updating the `factory` prop in `SplitFactoryProvider` replaces the current SDK instance, unlike `SplitFactory` where it is ignored.
2130
*
2231
* @see {@link https://help.split.io/hc/en-us/articles/360038825091-React-SDK#2-instantiate-the-sdk-and-create-a-new-split-client}
2332
*/

src/SplitFactoryProvider.tsx

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@ import React from 'react';
33
import { SplitComponent } from './SplitClient';
44
import { ISplitFactoryProps } from './types';
55
import { WARN_SF_CONFIG_AND_FACTORY } from './constants';
6-
import { getSplitFactory, destroySplitFactory, IFactoryWithClients, getSplitClient, getStatus, __factories } from './utils';
6+
import { getSplitFactory, destroySplitFactory, IFactoryWithClients, getSplitClient, getStatus } from './utils';
77
import { DEFAULT_UPDATE_OPTIONS } from './useSplitClient';
88

99
/**
1010
* 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,
1111
* and automatically destroy the SDK (shutdown and release resources) when it is unmounted or `config` prop updated. SplitFactoryProvider must wrap other library components and
1212
* functions since they access the Split Context and its properties (factory, client, isReady, etc).
1313
*
14-
* NOTE: Either pass a factory instance or a config object. If both are passed, the config object 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.
14+
* 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.
1616
*
1717
* @see {@link https://help.split.io/hc/en-us/articles/360038825091-React-SDK#2-instantiate-the-sdk-and-create-a-new-split-client}
1818
*/
@@ -44,7 +44,7 @@ export function SplitFactoryProvider(props: ISplitFactoryProps) {
4444

4545
// Effect to subscribe/unsubscribe to events
4646
React.useEffect(() => {
47-
const factory = config && __factories.get(config);
47+
const factory = config && getSplitFactory(config);
4848
if (factory) {
4949
const client = getSplitClient(factory);
5050
const status = getStatus(client);

src/__tests__/testUtils/utils.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ export function testAttributesBinding(Component: React.FunctionComponent<TestCom
110110
// With splitKey undefined, mainClient and client refer to the same client instance.
111111
wrapper = render(<Component splitKey={undefined} attributesFactory={{ at1: 'at1' }} attributesClient={{ at2: 'at2' }} testSwitch={attributesBindingSwitch} factory={factory} />);
112112

113-
wrapper.rerender(<Component splitKey={undefined} attributesFactory={undefined} attributesClient={{ at3: 'at3' }} testSwitch={attributesBindingSwitch} factory={factory} />);
114-
wrapper.rerender(<Component splitKey={undefined} attributesFactory={{ at4: 'at4' }} attributesClient={undefined} testSwitch={attributesBindingSwitch} factory={factory} />);
115-
wrapper.rerender(<Component splitKey={undefined} attributesFactory={undefined} attributesClient={undefined} testSwitch={attributesBindingSwitch} factory={factory} />);
113+
wrapper.rerender(<Component splitKey={undefined} attributesFactory={undefined} attributesClient={{ at3: 'at3' }} testSwitch={attributesBindingSwitch} factory={factory} />);
114+
wrapper.rerender(<Component splitKey={undefined} attributesFactory={{ at4: 'at4' }} attributesClient={undefined} testSwitch={attributesBindingSwitch} factory={factory} />);
115+
wrapper.rerender(<Component splitKey={undefined} attributesFactory={undefined} attributesClient={undefined} testSwitch={attributesBindingSwitch} factory={factory} />);
116116
}

src/withSplitFactory.tsx

+13-4
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,19 @@ import { SplitFactory } from './SplitFactory';
1010
* @param config Config object used to instantiate a Split factory
1111
* @param factory Split factory instance to use instead of creating a new one with the config object.
1212
*
13-
* @deprecated Use `SplitFactoryProvider` instead.
14-
* `SplitFactoryProvider` is a drop-in replacement of `SplitFactory` that properly handles side effects (factory creation and destruction) within the React component lifecycle, avoiding issues with factory recreation and memory leaks.
15-
* Note: There is a subtle breaking change in `SplitFactoryProvider`. When using the `config` prop, `factory` and `client` properties in the context are `null` in the first render, until the context is updated when some event is emitted on
16-
* the SDK main client (ready, ready from cache, timeout or update depending on the configuration of the `updateOnXXX` props of the component). This differs from the previous behavior where `factory` and `client` were immediately available.
13+
* @deprecated `withSplitFactory` will be removed in a future major release. We recommend replacing it with the new `SplitFactoryProvider` component.
14+
*
15+
* `SplitFactoryProvider` is a revised version of `SplitFactory` that properly handles SDK side effects (i.e., factory creation and destruction) within the React component lifecycle,
16+
* resolving memory leak issues in React development mode, strict mode and server-side rendering, and also ensuring that the SDK is updated if `config` or `factory` props change.
17+
*
18+
* Notable changes to consider when migrating:
19+
* - `SplitFactoryProvider` utilizes the React Hooks API, requiring React 16.8.0 or later, while `SplitFactory` is compatible with React 16.3.0 or later.
20+
* - When using the `config` prop with `SplitFactoryProvider`, the `factory` and `client` properties in `SplitContext` and the `manager` property in `useSplitManager` results
21+
* 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
22+
* the configuration of the `updateOn<Event>` props of the component). This differs from the previous behavior where `factory`, `client`, and `manager` were immediately available.
23+
* - Updating the `config` prop in `SplitFactoryProvider` reinitializes the SDK with the new configuration, while `SplitFactory` does not reinitialize the SDK. You should pass a
24+
* 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.
25+
* - Updating the `factory` prop in `SplitFactoryProvider` replaces the current SDK instance, unlike `SplitFactory` where it is ignored.
1726
*/
1827
export function withSplitFactory(config?: SplitIO.IBrowserSettings, factory?: SplitIO.IBrowserSDK, attributes?: SplitIO.Attributes) {
1928

0 commit comments

Comments
 (0)