Skip to content

Commit 06aedbc

Browse files
Merge branch 'migration-guide' into breaking-changes-explanation
2 parents 50b5028 + ceac3a7 commit 06aedbc

File tree

1 file changed

+71
-13
lines changed

1 file changed

+71
-13
lines changed

MIGRATION-GUIDE.md

Lines changed: 71 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,95 @@
1-
# React SDK v1.11.0: replace deprecated components with the new `SplitFactoryProvider` component
21

3-
The `SplitFactory` and `withSplitFactory` components have been deprecated since React SDK v1.11.0, and will be removed on a future major release.
2+
# Migrating to get React SDK v1.11.0 improvements: Replacing the deprecated `SplitFactory` and `withSplitFactory` components
43

5-
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, 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.
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.
611

712
Notable changes to consider when migrating:
813
- `SplitFactoryProvider` utilizes the React Hooks API, requiring React 16.8.0 or later, while `SplitFactory` is compatible with React 16.3.0 or later.
914

10-
- When using the `config` prop with `SplitFactoryProvider`, `factory` and `client` properties in `SplitContext` 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` and `client` 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.
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` and `client` 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.
1116

12-
- Updating the `config` prop in `SplitFactoryProvider` reinitializes the SDK with the new configuration, while `SplitFactory` does not reinitialize the SDK. It is recommended to 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` 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.
1318

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

16-
# React SDK v1.10.0: replace deprecated hooks with new ones
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 with the new `useSplitClient`, `useSplitTreatments`, and `useSplitManager` hooks
1773

18-
The `useClient`, `useTreatments` and `useManager` hooks have been deprecated since React SDK v1.10.0, and will be removed on a future major release.
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.
1975

2076
We recommend migrating to the new versions `useSplitClient`, `useSplitTreatments` and `useSplitManager` respectively, which provide a more flexible API:
2177

22-
- They accept extra optional parameters, `updateOnSdkReady`, `updateOnSdkReadyFromCache`, `updateOnSdkTimedout` and `updateOnSdkUpdate`, which enable to control when the hook updates the component. For example, you can set `updateOnSdkReady` 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.
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.
2381

24-
- 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 using the client's `ready` promise or event listeners. For example, you can show a loading spinner while the SDK is not ready, and use the `treatments` result to render the variants of your App once the SDK is ready.
82+
The usage of the new hooks is shown below:
2583

2684
```js
27-
const { client, isReady, isReadyFromCache, hasTimedout, lastUpdate } = useSplitClient();
28-
const { treatments, isReady, isReadyFromCache, hasTimedout, lastUpdate } = useSplitTreatments({ names: ['feature-flag-1'] });
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 });
2987
const { manager, isReady, isReadyFromCache, hasTimedout, lastUpdate } = useSplitManager();
3088
```
3189

3290

3391

34-
To refactor your existing code, replace:
92+
To migrate your existing code, replace:
3593

3694
```javascript
3795
const client = useClient(optionalSplitKey, optionalTrafficType, optionalAttributes);
@@ -47,7 +105,7 @@ const { treatments } = useSplitTreatments({ names: featureFlagNames, attributes:
47105
const { manager } = useSplitManager();
48106
```
49107

50-
and use the status properties to conditionally render your components. For example, do:
108+
and use the status properties to conditionally render your components. For example, use the following code:
51109

52110
```javascript
53111
const MyComponent = ({ userId }) => {

0 commit comments

Comments
 (0)