Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A useEffect's within useDecision will flap the auto update listeners on every render. #196

Closed
epatey opened this issue Apr 19, 2023 · 2 comments

Comments

@epatey
Copy link

epatey commented Apr 19, 2023

This useEffect will mount and execute on every single render. The root cause is that getCurrentDecision in useDecision is not wrapped in useCallback like it is in the other hooks.

  const getCurrentDecision: () => { decision: OptimizelyDecision } = () => ({
    decision: optimizely.decide(flagKey, options.decideOptions, overrides.overrideUserId, overrideAttrs),
  });

  ...

  useEffect(() => {
    // Subscribe to update after first datafile is fetched and readyPromise is resolved to avoid redundant rendering.
    if (optimizely.getIsReadyPromiseFulfilled() && options.autoUpdate) {
      return setupAutoUpdateListeners(optimizely, HookType.FEATURE, flagKey, hooksLogger, () => {
        setState(prevState => ({
          ...prevState,
          ...getCurrentDecision(),
        }));
      });
    }
    return (): void => { };
  }, [optimizely.getIsReadyPromiseFulfilled(), options.autoUpdate, optimizely, flagKey, getCurrentDecision]);
@mikechu-optimizely
Copy link
Contributor

Thanks for reporting this. I've created a work ticket (FSSDK-9624) to review.

@junaed-optimizely
Copy link
Contributor

This has been addressed already in #273 , and a patch release v3.2.1 is also out with the changes. Closing..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants