You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat!: options inheritance, useWhenProviderReady, suspend by default (#900)
* suspends while reconciling by default
* adds the ability to configure options at the context-provider level
* adds new `useWhenProviderReady` hook which force-suspend components
until the provider is ready (especially good for React 17 shortcomings
* updates README with FAQ/troubleshooting and more
---------
Signed-off-by: Todd Baert <[email protected]>
Co-authored-by: Lukas Reining <[email protected]>
-[Multiple Providers and Domains](#multiple-providers-and-domains)
56
+
-[Re-rendering with Context Changes](#re-rendering-with-context-changes)
57
+
-[Re-rendering with Flag Configuration Changes](#re-rendering-with-flag-configuration-changes)
58
+
-[Suspense Support](#suspense-support)
59
+
-[FAQ and troubleshooting](#faq-and-troubleshooting)
57
60
-[Resources](#resources)
58
61
59
62
## Quick start
@@ -87,7 +90,9 @@ The following list contains the peer dependencies of `@openfeature/react-sdk` wi
87
90
88
91
### Usage
89
92
90
-
The `OpenFeatureProvider` represents a scope for feature flag evaluations within a React application.
93
+
#### OpenFeatureProvider context provider
94
+
95
+
The `OpenFeatureProvider` is a [React context provider](https://react.dev/reference/react/createContext#provider) which represents a scope for feature flag evaluations within a React application.
91
96
It binds an OpenFeature client to all evaluations within child components, and allows the use of evaluation hooks.
92
97
The example below shows how to use the `OpenFeatureProvider` with OpenFeature's `InMemoryProvider`.
93
98
@@ -123,9 +128,15 @@ function App() {
123
128
</OpenFeatureProvider>
124
129
);
125
130
}
131
+
```
132
+
133
+
#### Evaluation hooks
126
134
135
+
Within the provider, you can use the various evaluation hooks to evaluate flags.
136
+
137
+
```tsx
127
138
function Page() {
128
-
// Use the "query-style" flag evaluation hook.
139
+
// Use the "query-style" flag evaluation hook, specifying a flag-key and a default value.
For more information about `domains`, refer to the [web SDK](https://github.com/open-feature/js-sdk/blob/main/packages/client/README.md).
185
194
186
-
### Re-rendering with Context Changes
195
+
####Re-rendering with Context Changes
187
196
188
197
By default, if the OpenFeature [evaluation context](https://openfeature.dev/docs/reference/concepts/evaluation-context) is modified, components will be re-rendered.
189
198
This is useful in cases where flag values are dependant on user-attributes or other application state (user logged in, items in card, etc).
190
-
You can disable this feature in the hook options:
199
+
You can disable this feature in the hook options (or in the [OpenFeatureProvider](#openfeatureprovider-context-provider)):
191
200
192
201
```tsx
193
202
function Page() {
@@ -200,11 +209,11 @@ function Page() {
200
209
201
210
For more information about how evaluation context works in the React SDK, see the documentation on OpenFeature's [static context SDK paradigm](https://openfeature.dev/specification/glossary/#static-context-paradigm).
202
211
203
-
### Re-rendering with Flag Configuration Changes
212
+
####Re-rendering with Flag Configuration Changes
204
213
205
214
By default, if the underlying provider emits a `ConfigurationChanged` event, components will be re-rendered.
206
215
This is useful if you want your UI to immediately reflect changes in the backend flag configuration.
207
-
You can disable this feature in the hook options:
216
+
You can disable this feature in the hook options (or in the [OpenFeatureProvider](#openfeatureprovider-context-provider)):
208
217
209
218
```tsx
210
219
function Page() {
@@ -217,11 +226,11 @@ function Page() {
217
226
218
227
Note that if your provider doesn't support updates, this configuration has no impact.
219
228
220
-
### Suspense Support
229
+
####Suspense Support
221
230
222
231
Frequently, providers need to perform some initial startup tasks.
223
-
It may be desireable not to display components with feature flags until this is complete.
224
-
Built-in [suspense](https://react.dev/reference/react/Suspense) support makes this easy:
232
+
It may be desireable not to display components with feature flags until this is complete, or when the context changes.
233
+
Built-in [suspense](https://react.dev/reference/react/Suspense) support makes this easy.
225
234
226
235
```tsx
227
236
function Content() {
@@ -252,6 +261,37 @@ function Fallback() {
252
261
// component to render before READY.
253
262
return <p>Waiting for provider to be ready...</p>;
254
263
}
264
+
265
+
```
266
+
267
+
This can be disabled in the hook options (or in the [OpenFeatureProvider](#openfeatureprovider-context-provider)).
268
+
269
+
## FAQ and troubleshooting
270
+
271
+
> I get an error that says something like: `A React component suspended while rendering, but no fallback UI was specified.`
272
+
273
+
The OpenFeature React SDK features built-in [suspense support](#suspense-support).
274
+
This means that it will render your loading fallback automatically while the your provider starts up, and during context reconciliation for any of your components using feature flags!
275
+
However, you will see this error if you neglect to create a suspense boundary around any components using feature flags; add a suspense boundary to resolve this issue.
276
+
Alternatively, you can disable this feature by setting `suspendWhileReconciling=false` and `suspendUntilReady=false` in the [evaluation hooks](#evaluation-hooks) or the [OpenFeatureProvider](#openfeatureprovider-context-provider) (which applies to all evaluation hooks in child components).
277
+
278
+
> I get odd rendering issues, or errors when components mount, if I use the suspense features.
279
+
280
+
In React 16/17's "Legacy Suspense", when a component suspends, its sibling components initially mount and then are hidden.
281
+
This can cause surprising effects and inconsistencies if sibling components are rendered while the provider is still getting ready.
282
+
To fix this, you can upgrade to React 18, which uses "Concurrent Suspense", in which siblings are not mounted until their suspended sibling resolves.
283
+
Alternatively, if you cannot upgrade to React 18, you can use the `useWhenProviderReady` utility hook in any sibling components to prevent them from mounting until the provider is ready.
284
+
285
+
> I am using multiple `OpenFeatureProvider` contexts, but they are sharing the same provider or evaluation context. Why?
286
+
287
+
The `OpenFeatureProvider` binds a `client` to all child components, but the provider and context associated with that client is controlled by the `domain` parameter.
288
+
This is consistent with all OpenFeature SDKs.
289
+
To scope an OpenFeatureProvider to a particular provider/context set the `domain` parameter on your `OpenFeatureProvider`:
0 commit comments