statsig-io / statsig-feedback

Issue Tracker for Statsig. Leave your feedback on anything we do!
5 stars 0 forks source link

Loading state for the React SDK #8

Closed matthewtole closed 2 years ago

matthewtole commented 3 years ago

It would be nice to be able to provide loading UI for the StatsigProvider while it waits to be initialized. Either by having a new prop that takes a React element that it renders while loading, or using React Suspense so we can handle the loading state elsewhere.

tore-statsig commented 3 years ago

Good ideas!

As it stands today, there are a few ways you can accomplish this. For any of these to work, you will have to remove the waitForInitialization property

1) You can use the useStatsig hook, which returns the statsig-js SDK statsig object when initialized, or undefined prior to that. You could use this to roll your own "waitForInitialize" with a loading indicator at the top level of your component tree

2) In each component that relies on useGate or useConfig, you can use the isLoading property to show a loading UI for that component while the SDK initializes.

Are you using the waitForInitialization parameter today? I could see adding a prop for rendering a component while you wait as a very valuable addition to the SDK in that case in particular.

In terms of supporting React Suspense, we can look into that once React 18 is released with support for Suspense.

tore-statsig commented 2 years ago

An initializationComponent has been added in the StatsigProvider in v1.0.0 of the react sdk:

https://github.com/statsig-io/react-sdk/releases/tag/v1.0.0

cc @matthewtole