|
1 | 1 |
|
| 2 | +# Migrating to React SDK v2.0.0 |
| 3 | + |
| 4 | +React SDK v2.0.0 has a few breaking changes that you should consider when migrating from a previous version. The main changes are: |
| 5 | + |
| 6 | +### • Deprecated `useClient`, `useTreatments`, and `useManager` hooks have been removed. |
| 7 | + |
| 8 | +Follow [this section](#migrating-to-get-react-sdk-v1100-improvements-replacing-the-deprecated-useclient-usetreatments-and-usemanager-hooks) to migrate to the new hooks `useSplitClient`, `useSplitTreatments`, and `useSplitManager`. |
| 9 | + |
| 10 | +### • Deprecated `SplitFactory` provider has been removed, `withSplitFactory` is deprecated, and `SplitFactoryProvider` doesn't accept `updateOn` props and a render function as children anymore. |
| 11 | + |
| 12 | +To migrate your existing code to the new version of `SplitFactoryProvider`, consider the following refactor example: |
| 13 | + |
| 14 | +```tsx |
| 15 | +const MyComponent = (props: ISplitContextValues) => { |
| 16 | + const { factory, client, isReady, isReadyFromCache, ... } = props; |
| 17 | + ... |
| 18 | +}; |
| 19 | + |
| 20 | +// if using SplitFactoryProvider v1.11.0 |
| 21 | +const App = () => { |
| 22 | + return ( |
| 23 | + <SplitFactoryProvider config={mySplitConfig} updateOnSdkUpdate={false} attributes={DEFAULT_CLIENT_ATTRIBUTES} > |
| 24 | + {MyComponent} |
| 25 | + </SplitFactoryProvider> |
| 26 | + ); |
| 27 | +}; |
| 28 | + |
| 29 | +// or SplitFactory |
| 30 | +const App = () => { |
| 31 | + return ( |
| 32 | + <SplitFactory config={mySplitConfig} updateOnSdkUpdate={false} attributes={DEFAULT_CLIENT_ATTRIBUTES} > |
| 33 | + {MyComponent} |
| 34 | + </SplitFactory> |
| 35 | + ); |
| 36 | +}; |
| 37 | + |
| 38 | +// or withSplitFactory |
| 39 | +const App = withSplitFactory(mySplitConfig, undefined, DEFAULT_CLIENT_ATTRIBUTES)( |
| 40 | + MyComponent, false /* updateOnSdkUpdate = false */ |
| 41 | +); |
| 42 | +``` |
| 43 | + |
| 44 | +should be refactored to: |
| 45 | + |
| 46 | +```tsx |
| 47 | +const MyComponent = () => { |
| 48 | + const props: ISplitContextValues = useSplitClient({ updateOnSdkUpdate: false }); |
| 49 | + const { factory, client, isReady, isReadyFromCache, ... } = props; |
| 50 | + ... |
| 51 | +}; |
| 52 | + |
| 53 | +const App = () => { |
| 54 | + return ( |
| 55 | + <SplitFactoryProvider config={mySplitConfig} attributes={DEFAULT_CLIENT_ATTRIBUTES} > |
| 56 | + <MyComponent /> |
| 57 | + </SplitFactoryProvider> |
| 58 | + ); |
| 59 | +}; |
| 60 | +``` |
| 61 | + |
| 62 | +Notice that `MyComponent` was refactored to use the `useSplitClient` hook and is passed as a React JSX element rather than a render function. The `useSplitClient` hook is called without providing a `splitKey` param. This means that the default client (whose key is set in the `core.key` property of the `mySplitConfig` object) will be used, and the `updateOn` and `attributes` props are passed as options to the hook. |
| 63 | + |
| 64 | +### • High-Order-Components (`withSplitClient`, `withSplitTreatments`) and components that accept a render function as child component (`SplitTreatments`, and `SplitClient`) have been deprecated and might be removed in a future major release. |
| 65 | + |
| 66 | +The deprecation is intended to simplify the API and discourage using old patterns (HOCs and render props) in favor of the *hook* alternatives, to take advantage of React optimizations. |
| 67 | + |
| 68 | +To migrate your existing code based on `withSplitClient` or `SplitClient`, consider the following refactor using the `useSplitClient` hook: |
| 69 | + |
| 70 | +```tsx |
| 71 | +const MyComponent = (props: ISplitContextValues) => { |
| 72 | + const { client, isReady, ... } = props; |
| 73 | + ... |
| 74 | +}; |
| 75 | + |
| 76 | +const App = withSplitFactory(mySplitConfig)( |
| 77 | + withSplitClient(OTHER_KEY, OTHER_KEY_ATTRIBUTES)( |
| 78 | + MyComponent, undefined, undefined, undefined, false /* updateOnSdkReadyFromCache = false */ |
| 79 | + ) |
| 80 | +); |
| 81 | + |
| 82 | +// or |
| 83 | +const App = () => { |
| 84 | + return ( |
| 85 | + <SplitFactory config={mySplitConfig} > |
| 86 | + <SplitClient splitKey={OTHER_KEY} attributes={OTHER_KEY_ATTRIBUTES} updateOnSdkReadyFromCache={false} > |
| 87 | + {MyComponent} |
| 88 | + </SplitClient> |
| 89 | + </SplitFactory> |
| 90 | + ) |
| 91 | +}; |
| 92 | +``` |
| 93 | + |
| 94 | +should be refactored to: |
| 95 | + |
| 96 | +```tsx |
| 97 | +const MyComponent = () => { |
| 98 | + const props: ISplitContextValues = useSplitClient({ splitKey: OTHER_KEY, attributes: OTHER_KEY_ATTRIBUTES, updateOnSdkReadyFromCache: false }); |
| 99 | + const { client, isReady, ... } = props; |
| 100 | + ... |
| 101 | +}; |
| 102 | + |
| 103 | +const App = () => { |
| 104 | + return ( |
| 105 | + <SplitFactoryProvider config={mySplitConfig} > |
| 106 | + <MyComponent /> |
| 107 | + </SplitFactory> |
| 108 | + ) |
| 109 | +}; |
| 110 | +``` |
| 111 | + |
| 112 | +To migrate your existing code based on `withSplitTreatments` or `SplitTreatments`, consider the following refactor using the `useSplitTreatments` hook: |
| 113 | + |
| 114 | +```tsx |
| 115 | +const MyComponent = (props: ISplitTreatmentsChildProps) => { |
| 116 | + const { treatments, isReady, ... } = props; |
| 117 | + ... |
| 118 | +}; |
| 119 | + |
| 120 | +const App = withSplitFactory(mySplitConfig)( |
| 121 | + withSplitClient(OTHER_KEY)( |
| 122 | + withSplitTreatments(FEATURE_FLAG_NAMES, ATTRIBUTES)( |
| 123 | + MyComponent |
| 124 | + ) |
| 125 | + ) |
| 126 | +); |
| 127 | + |
| 128 | +// or |
| 129 | +const App = () => { |
| 130 | + return ( |
| 131 | + <SplitFactory config={mySplitConfig} > |
| 132 | + <SplitClient splitKey={OTHER_KEY} > |
| 133 | + <SplitTreatments names={FEATURE_FLAG_NAMES} attributes={ATTRIBUTES} > |
| 134 | + {MyComponent} |
| 135 | + </SplitTreatments> |
| 136 | + </SplitClient> |
| 137 | + </SplitFactory> |
| 138 | + ) |
| 139 | +}; |
| 140 | +``` |
| 141 | + |
| 142 | +should be refactored to: |
| 143 | + |
| 144 | +```tsx |
| 145 | +const MyComponent = () => { |
| 146 | + const props: ISplitTreatmentsChildProps = useSplitTreatments({ splitKey: OTHER_KEY, names: FEATURE_FLAG_NAMES, attributes: ATTRIBUTES }); |
| 147 | + const { treatments, isReady, ... } = props; |
| 148 | + ... |
| 149 | +}; |
| 150 | + |
| 151 | +const App = () => { |
| 152 | + return ( |
| 153 | + <SplitFactoryProvider config={mySplitConfig} > |
| 154 | + <MyComponent /> |
| 155 | + </SplitFactory> |
| 156 | + ) |
| 157 | +}; |
| 158 | +``` |
| 159 | + |
| 160 | +### • Renamed `SplitSdk` function to `SplitFactory`. |
| 161 | + |
| 162 | +If you are using the `SplitSdk` function to create a factory and pass it to the `SplitFactoryProvider` component, you should rename it to `SplitFactory`. For example: |
| 163 | + |
| 164 | +```tsx |
| 165 | +import { SplitSdk, SplitFactoryProvider } from '@splitsoftware/splitio-react'; |
| 166 | + |
| 167 | +const myFactory = SplitSdk(mySplitConfig); |
| 168 | + |
| 169 | +const App = () => { |
| 170 | + return ( |
| 171 | + <SplitFactoryProvider factory={myFactory} > |
| 172 | + <MyComponent /> |
| 173 | + </SplitFactoryProvider> |
| 174 | + ); |
| 175 | +}; |
| 176 | +``` |
| 177 | + |
| 178 | +should be refactored to: |
| 179 | + |
| 180 | +```tsx |
| 181 | +import { SplitFactory, SplitFactoryProvider } from '@splitsoftware/splitio-react'; |
| 182 | + |
| 183 | +const myFactory = SplitFactory(mySplitConfig); |
| 184 | + |
| 185 | +const App = () => { |
| 186 | + return ( |
| 187 | + <SplitFactoryProvider factory={myFactory} > |
| 188 | + <MyComponent /> |
| 189 | + </SplitFactoryProvider> |
| 190 | + ); |
| 191 | +}; |
| 192 | +``` |
| 193 | + |
| 194 | +### • Traffic type cannot be bound to SDK clients anymore. |
| 195 | + |
| 196 | +If you were passing the `trafficType` to the SDK config, `useSplitClient` hook, or `useTrack` hook, you should remove it. The `trafficType` must now be passed as the first argument of the `track` method. For example: |
| 197 | + |
| 198 | +```tsx |
| 199 | +const mySplitConfig = { |
| 200 | + core: { |
| 201 | + authorizationKey: YOUR_CLIENT_SIDE_SDK_KEY, |
| 202 | + key: USER_KEY, |
| 203 | + trafficType: 'user' |
| 204 | + } |
| 205 | +} |
| 206 | + |
| 207 | +const MyComponent = () => { |
| 208 | + const track = useTrack(); |
| 209 | + const accountTrack = useTrack(ACCOUNT_KEY, 'account'); |
| 210 | + |
| 211 | + useEffect(() => { |
| 212 | + track('my_event'); |
| 213 | + accountTrack('my_event'); |
| 214 | + }, []); |
| 215 | + |
| 216 | + ... |
| 217 | +}; |
| 218 | + |
| 219 | +const App = () => { |
| 220 | + return ( |
| 221 | + <SplitFactoryProvider config={mySplitConfig} > |
| 222 | + <MyComponent /> |
| 223 | + </SplitFactory> |
| 224 | + ) |
| 225 | +}; |
| 226 | +``` |
| 227 | + |
| 228 | +should be refactored to: |
| 229 | + |
| 230 | +```tsx |
| 231 | +const mySplitConfig = { |
| 232 | + core: { |
| 233 | + authorizationKey: YOUR_CLIENT_SIDE_SDK_KEY, |
| 234 | + key: USER_KEY |
| 235 | + } |
| 236 | +} |
| 237 | + |
| 238 | +const MyComponent = () => { |
| 239 | + const track = useTrack(); |
| 240 | + const accountTrack = useTrack(ACCOUNT_KEY); |
| 241 | + |
| 242 | + useEffect(() => { |
| 243 | + track('user', 'my_event'); |
| 244 | + accountTrack('account', 'my_event'); |
| 245 | + }, []); |
| 246 | + ... |
| 247 | +}; |
| 248 | + |
| 249 | +const App = () => { |
| 250 | + return ( |
| 251 | + <SplitFactoryProvider config={mySplitConfig} > |
| 252 | + <MyComponent /> |
| 253 | + </SplitFactory> |
| 254 | + ) |
| 255 | +}; |
| 256 | +``` |
| 257 | + |
2 | 258 | # Migrating to get React SDK v1.11.0 improvements: Replacing the deprecated `SplitFactory` and `withSplitFactory` components
|
3 | 259 |
|
4 | 260 | 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.
|
|
0 commit comments