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

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

Open epatey opened this issue 2 years ago • 1 comments

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]);

epatey avatar Apr 19 '23 20:04 epatey

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

mikechu-optimizely avatar Aug 28 '23 19:08 mikechu-optimizely

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

junaed-optimizely avatar Aug 15 '24 17:08 junaed-optimizely