Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(react-bindings): multiple client async start handling while loading #17

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 15 additions & 14 deletions packages/react-bindings/src/useStatsigInternalClientFactoryAsync.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useMemo, useRef, useState } from 'react';
import { useEffect, useRef, useState } from 'react';

import { Log, StatsigUser, _getInstance } from '@statsig/client-core';
import { StatsigClient, StatsigOptions } from '@statsig/js-client';
Expand All @@ -14,24 +14,25 @@ export function useStatsigInternalClientFactoryAsync<T extends StatsigClient>(
args: FactoryArgs,
): { isLoading: boolean; client: T } {
const [isLoading, setIsLoading] = useState(true);
const clientRef = useRef<T | null>(_getInstance(args.sdkKey) as T | null);

const client = useMemo(() => {
if (clientRef.current) {
return clientRef.current;
const clientRef = useRef<T>(
(_getInstance(args.sdkKey) as T | null) ?? factory(args),
);

useEffect(() => {
// already initializing or initialized
if (clientRef.current.loadingStatus !== 'Uninitialized') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you better outline what it is you are trying to solve? This change means that in strict mode, we will immediately render, regardless of the SDKs status.

Adding this logic means that we will just set isLoading to false on the re-render as its loadingStatus will be "Loading" and therefore pass this check.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This logic is intended to prevent multiple SDK clients/network requests being created.

In React Strict mode, a re-render occurs, blowing away the previous state.
The logic here is supposed to create the client on the first render, then reuse it on the second.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct.
As far as I could tell, the old logic tried to do the same.

setIsLoading(false);
return;
}

const inst = factory(args);

clientRef.current = inst;

inst
clientRef.current
.initializeAsync()
.catch(Log.error)
.finally(() => setIsLoading(false));

return inst;
}, []);

return { client, isLoading };
return {
client: clientRef.current,
isLoading,
};
}
7 changes: 6 additions & 1 deletion samples/react/src/__tests__/HomePage.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { render, waitFor } from '@testing-library/react';
import fetchMock from 'jest-fetch-mock';
import { StrictMode } from 'react';
import { InitResponse } from 'statsig-test-helpers';

import HomePage from '../HomePage';
Expand All @@ -10,7 +11,11 @@ describe('App', () => {
});

it('renders the Passing value', async () => {
const { getByText } = render(<HomePage />);
const { getByText } = render(
<StrictMode>
<HomePage />
</StrictMode>,
);

const result = await waitFor(() => getByText('Passing'));
expect(result).toBeDefined();
Expand Down
8 changes: 6 additions & 2 deletions samples/react/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import '@fontsource/roboto/400.css';
import '@fontsource/roboto/500.css';
import '@fontsource/roboto/700.css';
import { Box } from '@mui/material';
import { ReactNode, lazy } from 'react';
import { ReactNode, StrictMode, lazy } from 'react';
import * as ReactDOM from 'react-dom/client';
import {
RouteObject,
Expand Down Expand Up @@ -89,4 +89,8 @@ function App() {
);
}

root.render(<App />);
root.render(
<StrictMode>
<App />
</StrictMode>,
);