Skip to content

Commit 18cef96

Browse files
committed
Add logged warning when no domain nor defaultContext
1 parent 20631c3 commit 18cef96

1 file changed

Lines changed: 20 additions & 3 deletions

File tree

packages/react/src/context/use-context-mutator.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useCallback, useContext } from 'react';
1+
import { useCallback, useContext, useEffect, useState } from 'react';
22
import type { EvaluationContext } from '@openfeature/web-sdk';
33
import { OpenFeature } from '@openfeature/web-sdk';
44
import { Context } from '../internal';
@@ -36,8 +36,25 @@ export function useContextMutator(options: ContextMutationOptions = { defaultCon
3636
const { client } = useContext(Context) || {};
3737
const domain = client?.metadata.domain;
3838

39-
// TODO: If `defaultContext` is `false`, and we don't have a `client`,
40-
// should we throw an error (that we're not inside `<OpenFeatureProvider/>`)?
39+
// TODO: Replace this warning with a thrown error in a future major release,
40+
// to match the behavior of `useOpenFeatureProvider` + `useOpenFeatureClient`,
41+
// when `defaultContext` isn't explicitly set to true.
42+
const [warned, setWarned] = useState(false);
43+
useEffect(() => {
44+
if (options.defaultContext || domain) {
45+
if (warned) {
46+
setWarned(false);
47+
}
48+
return;
49+
}
50+
51+
if (!warned) {
52+
console.warn(
53+
'[useContextMutator] No domain available from OpenFeature context; are you using <OpenFeatureProvider/>? setContext will mutate the default context, as if `defaultContext: true` were set. This may result in a thrown error in the future.',
54+
);
55+
setWarned(true);
56+
}
57+
}, [warned]);
4158

4259
const setContext = useCallback(
4360
async (

0 commit comments

Comments
 (0)