@flipfeatureflag/react 0.1.8 → 0.1.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +3 -3
  2. package/src/index.tsx +22 -4
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@flipfeatureflag/react",
3
- "version": "0.1.8",
3
+ "version": "0.1.9",
4
4
  "description": "flipFeatureFlag React SDK",
5
5
  "main": "dist/index.cjs",
6
6
  "module": "dist/index.mjs",
@@ -20,7 +20,7 @@
20
20
  "react": ">=17"
21
21
  },
22
22
  "dependencies": {
23
- "@flipfeatureflag/core": "0.1.7",
24
- "@flipfeatureflag/js": "0.1.8"
23
+ "@flipfeatureflag/core": "0.1.8",
24
+ "@flipfeatureflag/js": "0.1.9"
25
25
  }
26
26
  }
package/src/index.tsx CHANGED
@@ -109,19 +109,37 @@ export function useFlagsStatus(): FlagsStatus {
109
109
  return React.useContext(FlipFlagStatusContext);
110
110
  }
111
111
 
112
- export function useFlag(flagKey: string, defaultValue = false): boolean {
112
+ export interface FlagValueResult<T = SdkFlagEvaluation["value"]> {
113
+ value: T;
114
+ enabled: boolean;
115
+ }
116
+
117
+ function toFlagResult(evaluation: SdkFlagEvaluation, known: boolean): FlagValueResult {
118
+ const enabled = known && evaluation.reason !== "disabled";
119
+ return {
120
+ value: evaluation.value,
121
+ enabled,
122
+ };
123
+ }
124
+
125
+ export function useFlag(
126
+ flagKey: string,
127
+ defaultValue: SdkFlagEvaluation["value"] = false,
128
+ ): FlagValueResult {
113
129
  const client = useFlipFlagClient();
114
- const [value, setValue] = useState(() => client.isEnabled(flagKey, defaultValue));
130
+ const [result, setResult] = useState(() =>
131
+ toFlagResult(client.getVariant(flagKey, defaultValue), client.hasFlag(flagKey)),
132
+ );
115
133
 
116
134
  useEffect(() => {
117
135
  const update = () => {
118
- setValue(client.isEnabled(flagKey, defaultValue));
136
+ setResult(toFlagResult(client.getVariant(flagKey, defaultValue), client.hasFlag(flagKey)));
119
137
  };
120
138
  client.on("update", update);
121
139
  return () => client.off("update", update);
122
140
  }, [client, flagKey, defaultValue]);
123
141
 
124
- return value;
142
+ return result;
125
143
  }
126
144
 
127
145
  export function useVariant(flagKey: string, defaultValue: SdkFlagEvaluation["value"] = false): SdkFlagEvaluation {