Skip to content

Commit 8a4dd8e

Browse files
Merge pull request #54 from launchdarkly/yus/sc-148098/update-react-sdk-to-react-18
[sc-148098] Upgrade to react 18
2 parents 883aa3e + 1b16c4e commit 8a4dd8e

File tree

8 files changed

+35
-42
lines changed

8 files changed

+35
-42
lines changed

.prettierrc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"singleQuote": true,
33
"trailingComma": "all",
4-
"printWidth": 120
4+
"printWidth": 120,
5+
"arrowParens": "always"
56
}

package.json

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,27 +37,24 @@
3737
},
3838
"homepage": "https://github.com/launchdarkly/react-client-sdk",
3939
"devDependencies": {
40-
"@testing-library/jest-dom": "^5.16.1",
41-
"@testing-library/react": "^12.1.2",
42-
"@types/enzyme": "^3.10.3",
40+
"@testing-library/jest-dom": "^5.16.4",
41+
"@testing-library/react": "^13.0.1",
4342
"@types/hoist-non-react-statics": "^3.3.1",
4443
"@types/jest": "^27.0.3",
4544
"@types/lodash.camelcase": "^4.3.6",
4645
"@types/prop-types": "^15.7.4",
47-
"@types/react": "^17.0.37",
48-
"@types/react-dom": "^17.0.11",
49-
"@types/react-test-renderer": "^17.0.1",
46+
"@types/react": "^18.0.3",
47+
"@types/react-dom": "^18.0.0",
48+
"@types/react-test-renderer": "^18.0.0",
5049
"@types/uuid": "^3.4.5",
51-
"enzyme": "^3.10.0",
52-
"enzyme-adapter-react-16": "^1.14.0",
5350
"jest": "^27.4.4",
5451
"jest-environment-jsdom-global": "^3.0.0",
5552
"jest-junit": "^13.0.0",
5653
"prettier": "^1.18.2",
5754
"prop-types": "^15.7.2",
58-
"react": "^17.0.0",
59-
"react-dom": "^17.0.0",
60-
"react-test-renderer": "^17.0.2",
55+
"react": "^18.0.0",
56+
"react-dom": "^18.0.0",
57+
"react-test-renderer": "^18.0.0",
6158
"rimraf": "^3.0.0",
6259
"ts-jest": "^27.1.1",
6360
"tslint": "^6.1.3",
@@ -72,7 +69,7 @@
7269
"uuid": "^3.3.2"
7370
},
7471
"peerDependencies": {
75-
"react": "^16.6.3 || ^17.0.0",
76-
"react-dom": "^16.8.4 || ^17.0.0"
72+
"react": "^16.6.3 || ^17.0.0 || ^18.0.0",
73+
"react-dom": "^16.8.4 || ^17.0.0 || ^18.0.0"
7774
}
7875
}

setupTests.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1 @@
1-
const enzyme = require('enzyme');
2-
const Adapter = require('enzyme-adapter-react-16');
31
require('@testing-library/jest-dom/extend-expect');
4-
5-
enzyme.configure({ adapter: new Adapter() });

src/asyncWithLDProvider.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ const renderWithConfig = async (config: AsyncProviderConfig) => {
3030

3131
const { getByText } = render(
3232
<LDProvider>
33-
<Consumer>{value => <span>Received: {JSON.stringify(value.flags)}</span>}</Consumer>
33+
<Consumer>{(value) => <span>Received: {JSON.stringify(value.flags)}</span>}</Consumer>
3434
</LDProvider>,
3535
);
3636

src/asyncWithLDProvider.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useState, useEffect, FunctionComponent } from 'react';
1+
import React, { useState, useEffect, ReactNode } from 'react';
22
import { LDFlagSet, LDFlagChangeset } from 'launchdarkly-js-client-sdk';
33
import { AsyncProviderConfig, defaultReactOptions } from './types';
44
import { Provider } from './context';
@@ -34,7 +34,7 @@ export default async function asyncWithLDProvider(config: AsyncProviderConfig) {
3434
const reactOptions = { ...defaultReactOptions, ...userReactOptions };
3535
const { ldClient } = await initLDClient(clientSideID, user, reactOptions, options, targetFlags);
3636

37-
const LDProvider: FunctionComponent = ({ children }) => {
37+
const LDProvider = ({ children }: { children: ReactNode }) => {
3838
const [ldData, setLDData] = useState({
3939
flags: fetchFlags(ldClient, reactOptions, targetFlags),
4040
ldClient,
@@ -45,14 +45,14 @@ export default async function asyncWithLDProvider(config: AsyncProviderConfig) {
4545
const { bootstrap } = options;
4646
if (bootstrap && bootstrap !== 'localStorage') {
4747
const bootstrappedFlags = reactOptions.useCamelCaseFlagKeys ? camelCaseKeys(bootstrap) : bootstrap;
48-
setLDData(prev => ({ ...prev, flags: bootstrappedFlags }));
48+
setLDData((prev) => ({ ...prev, flags: bootstrappedFlags }));
4949
}
5050
}
5151

5252
ldClient.on('change', (changes: LDFlagChangeset) => {
5353
const flattened: LDFlagSet = getFlattenedFlagsFromChangeset(changes, targetFlags, reactOptions);
5454
if (Object.keys(flattened).length > 0) {
55-
setLDData(prev => ({ ...prev, flags: { ...prev.flags, ...flattened } }));
55+
setLDData((prev) => ({ ...prev, flags: { ...prev.flags, ...flattened } }));
5656
}
5757
});
5858
}, []);

src/initLDClient.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const initLDClient = async (
2525
const allOptions = { wrapperName: 'react-client-sdk', wrapperVersion: version, ...options };
2626
const ldClient = ldClientInitialize(clientSideID, user, allOptions);
2727

28-
return new Promise<AllFlagsLDClient>(resolve => {
28+
return new Promise<AllFlagsLDClient>((resolve) => {
2929
ldClient.on('ready', () => {
3030
const flags = fetchFlags(ldClient, reactOptions, targetFlags);
3131
resolve({ flags, ldClient });

src/provider.test.tsx

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
jest.mock('./initLDClient', () => jest.fn());
22
jest.mock('./context', () => ({ Provider: 'Provider' }));
33

4-
import * as React from 'react';
4+
import React, { Component } from 'react';
55
import { create } from 'react-test-renderer';
6-
import { shallow } from 'enzyme';
76
import { LDClient, LDFlagChangeset, LDFlagSet, LDOptions, LDUser } from 'launchdarkly-js-client-sdk';
87
import initLDClient from './initLDClient';
98
import { LDReactOptions, EnhancedComponent, defaultReactOptions, ProviderConfig } from './types';
@@ -80,7 +79,7 @@ describe('LDProvider', () => {
8079
const user1: LDUser = { key: 'yus', name: 'yus ng' };
8180
const user2: LDUser = { key: 'launch', name: 'darkly' };
8281
const options: LDOptions = { bootstrap: {} };
83-
const ldClient: Promise<LDClient> = new Promise(async resolve => {
82+
const ldClient: Promise<LDClient> = new Promise(async (resolve) => {
8483
resolve((await initLDClient(clientSideID, user1, defaultReactOptions, options, undefined)).ldClient);
8584

8685
return;
@@ -101,7 +100,7 @@ describe('LDProvider', () => {
101100
test('ld client is created if passed in promise resolves as undefined', async () => {
102101
const user: LDUser = { key: 'yus', name: 'yus ng' };
103102
const options: LDOptions = { bootstrap: {} };
104-
const ldClient: Promise<undefined> = new Promise(async resolve => {
103+
const ldClient: Promise<undefined> = new Promise(async (resolve) => {
105104
resolve(undefined);
106105

107106
return;
@@ -129,8 +128,8 @@ describe('LDProvider', () => {
129128
<App />
130129
</LDProvider>
131130
);
132-
const component = shallow(LaunchDarklyApp, { disableLifecycleMethods: true });
133-
const initialState = component.state() as HocState;
131+
const component = create(LaunchDarklyApp).toTree()?.instance as Component;
132+
const initialState = component.state as HocState;
134133

135134
expect(initialState.flags).toEqual({});
136135
});
@@ -154,8 +153,8 @@ describe('LDProvider', () => {
154153
<App />
155154
</LDProvider>
156155
);
157-
const component = shallow(LaunchDarklyApp, { disableLifecycleMethods: true });
158-
const initialState = component.state() as HocState;
156+
const component = create(LaunchDarklyApp).toTree()?.instance as Component;
157+
const initialState = component.state as HocState;
159158

160159
expect(mockInitLDClient).not.toHaveBeenCalled();
161160
expect(initialState.flags).toEqual({ testFlag: true, anotherTestFlag: false });
@@ -178,8 +177,8 @@ describe('LDProvider', () => {
178177
<App />
179178
</LDProvider>
180179
);
181-
const component = shallow(LaunchDarklyApp, { disableLifecycleMethods: true });
182-
const initialState = component.state() as HocState;
180+
const component = create(LaunchDarklyApp).toTree()?.instance as Component;
181+
const initialState = component.state as HocState;
183182

184183
expect(mockInitLDClient).not.toHaveBeenCalled();
185184
expect(initialState.flags).toEqual({ 'test-flag': true, 'another-test-flag': false });
@@ -200,8 +199,8 @@ describe('LDProvider', () => {
200199
<App />
201200
</LDProvider>
202201
);
203-
const component = shallow(LaunchDarklyApp, { disableLifecycleMethods: true });
204-
const initialState = component.state() as HocState;
202+
const component = create(LaunchDarklyApp).toTree()?.instance as Component;
203+
const initialState = component.state as HocState;
205204

206205
expect(mockInitLDClient).not.toHaveBeenCalled();
207206
expect(initialState.flags).toEqual({ testFlag: true, anotherTestFlag: false });
@@ -221,8 +220,8 @@ describe('LDProvider', () => {
221220
<App />
222221
</LDProvider>
223222
);
224-
const component = shallow(LaunchDarklyApp, { disableLifecycleMethods: true });
225-
const initialState = component.state() as HocState;
223+
const component = create(LaunchDarklyApp).toTree()?.instance as Component;
224+
const initialState = component.state as HocState;
226225

227226
expect(mockInitLDClient).not.toHaveBeenCalled();
228227
expect(initialState.flags).toEqual({ testFlag: true, anotherTestFlag: false });
@@ -239,8 +238,8 @@ describe('LDProvider', () => {
239238
<App />
240239
</LDProvider>
241240
);
242-
const component = shallow(LaunchDarklyApp, { disableLifecycleMethods: true });
243-
const initialState = component.state() as HocState;
241+
const component = create(LaunchDarklyApp).toTree()?.instance as Component;
242+
const initialState = component.state as HocState;
244243

245244
expect(mockInitLDClient).not.toHaveBeenCalled();
246245
expect(initialState.flags).toEqual({});

src/provider.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as React from 'react';
1+
import React, { Component, PropsWithChildren } from 'react';
22
import { LDClient, LDFlagSet, LDFlagChangeset } from 'launchdarkly-js-client-sdk';
33
import { EnhancedComponent, ProviderConfig, defaultReactOptions } from './types';
44
import { Provider, LDContext as HocState } from './context';
@@ -22,7 +22,7 @@ import { camelCaseKeys, fetchFlags, getFlattenedFlagsFromChangeset } from './uti
2222
* within your application. This provider is used inside the `withLDProviderHOC` and can be used instead to initialize
2323
* the `launchdarkly-js-client-sdk`. For async initialization, check out the `asyncWithLDProvider` function
2424
*/
25-
class LDProvider extends React.Component<ProviderConfig, HocState> implements EnhancedComponent {
25+
class LDProvider extends Component<PropsWithChildren<ProviderConfig>, HocState> implements EnhancedComponent {
2626
readonly state: Readonly<HocState>;
2727

2828
constructor(props: ProviderConfig) {

0 commit comments

Comments
 (0)