Skip to content

Commit ceac3a7

Browse files
Add migration example for SplitFactoryProvider
1 parent 2bbe2ca commit ceac3a7

File tree

1 file changed

+52
-1
lines changed

1 file changed

+52
-1
lines changed

MIGRATION-GUIDE.md

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,63 @@ We recommend migrating to the new `SplitFactoryProvider` component instead. This
1212
Notable changes to consider when migrating:
1313
- `SplitFactoryProvider` utilizes the React Hooks API, requiring React 16.8.0 or later, while `SplitFactory` is compatible with React 16.3.0 or later.
1414

15-
- 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.
1616

1717
- 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.
1818

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

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+
2172
# 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
2273

2374
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.

0 commit comments

Comments
 (0)