react-sdk icon indicating copy to clipboard operation
react-sdk copied to clipboard

[BUG] useFeature returns false directly when the return datafile indicates true

Open yslkmart opened this issue 9 months ago • 3 comments

Is there an existing issue for this?

  • [x] I have searched the existing issues

SDK Version

"@optimizely/react-sdk": "^3.2.2",

Current Behavior

I'm using useFeature hook to get the feature flag and later use react query to call diff endpoint per feature flag setting.

When the component renders, I get isEnabled returned from the hook as false but later on all true. The setting on the Optimizely end for the flag is true and I can see it from the returned datafile as well.

I also tried to use clientReady, but it was true ever since component rendered.

Is this the default behaviour? I'm expecting isEnabled to be undefined while the client sdk is processing the datafile to get the isEnabled state.

Thanks,

Expected Behavior

useFeature hook should return undefined while processing

Steps To Reproduce

just use useFeature hook for a specific feature

React Framework

"react": "^18.3.1",

Browsers impacted

No response

Link

No response

Logs

No response

Severity

No response

Workaround/Solution

No response

Recent Change

No response

Conflicts

No response

yslkmart avatar Mar 06 '25 04:03 yslkmart

Hey @yslkmart. Here are my findings on this -

  1. clientReady should not be ready from the beginning. And we made some changes regarding that. #285. v3.2.3 contains the change. You are one patch short from that.
  2. useFeature is designed this way from the beginning. If the client is not ready, it returns false as a default. I can understand why that could be misleading. So Your proposal on using undefined instead of false might be a better idea. But I don't see it as a bug necessarily. It could be an enhancement request. I have created an internal ticket FSSDK-11245 to track this.

Thanks for the issue!

junaed-optimizely avatar Mar 06 '25 11:03 junaed-optimizely

@junaed-optimizely Thanks for the reply. I'm using ^3.2.2 so I think I'm actually using v3.2.3? but still clientReady is instantly true when component renders while isEnabled is false and in the datafile it's true.

So it sounds like getting isEnabled as false initially from useFeature is by design? In our case, we actually want three states for isEnabled i.e undefined when client is not ready, true and false when it is. Is there an existing workaround where we know when the client is not ready? clientReady doesn't seem to work. Currently, we just wait for a sec before reading isEnabled.

Thanks,

yslkmart avatar Mar 06 '25 21:03 yslkmart

@yslkmart yes, in that case you are using 3.2.3. In that case you might be waiting for the client to get ready before rendering the component.

Either way, I think isEnabled should be undefined ( not false) while the SDK is calculating..

junaed-optimizely avatar Mar 10 '25 13:03 junaed-optimizely