Skip to content
This repository has been archived by the owner on Apr 27, 2022. It is now read-only.

When using hooks adding parameters causes infinite rendering loop #54

Closed
scottc5 opened this issue Nov 10, 2021 · 7 comments · Fixed by #57
Closed

When using hooks adding parameters causes infinite rendering loop #54

scottc5 opened this issue Nov 10, 2021 · 7 comments · Fixed by #57

Comments

@scottc5
Copy link

scottc5 commented Nov 10, 2021

I am using v1.2.1 and get the same response on IOS and Android. When using hooks like useRewardedAd, if I include parameters (doesnt seem to matter which ones, though my goal was to use requestNonPersonalizedAdsOnly) then the app goes into an infinite rendering loop. Below is an example setting almost all of the parameters, but I also just tried the requestNonPersonalizedAdsOnly one and got the same result.
const op = { loadOnMounted: true, showOnLoaded: false, loadOnDismissed:true, requestOptions: { requestNonPersonalizedAdsOnly: true}}; const { adLoadError, adLoaded, show, load, reward, adPresented, adDismissed } = useRewardedAd(TestIds.REWARDED, op);

Or this code:
const requestOptions = { requestNonPersonalizedAdsOnly: true, }; const { adLoadError, adLoaded, show, load, reward, adPresented, adDismissed } = useRewardedAd(TestIds.REWARDED, requestOptions);
image

@wjaykim
Copy link
Collaborator

wjaykim commented Nov 11, 2021

requestOptions is property of option object. Second parameter of the hook should be option object, that means you should wrap requestOptions with curly braces, so the code should be like:

const {} = useRewardedAd(UNITID, {
  requestOptions
});

@ShivamJoker
Copy link

ShivamJoker commented Nov 16, 2021

@wjaykim you missed on his first example.

const {} = useRewardedAd(UNITID, {
  loadOnMounted: false,
});

This puts the app in loop is there anything else to be noted ?

Warning: Maximum update depth exceeded. This can happen when a component calls setState inside useEffect, but useEffect either doesn't have a dependency array, or one of the dependencies changes on every render.

@wjaykim
Copy link
Collaborator

wjaykim commented Nov 20, 2021

Currently I am investigating why this happens. Meanwhile, there is temporary solution for this. Declare your options outside of react component, or memoize your options like

const hookOptions: FullScreenAdOptions = useMemo(
    () => ({
      loadOnDismissed: true,
      requestOptions: {
        requestNonPersonalizedAdsOnly: true,
      },
    }),
    []
  );

then infinite loop will not happen.

@ShivamJoker
Copy link

@wjaykim how about we put the memo in the library where we are loading the config itself so it doesn't go in render loop ?

@wjaykim
Copy link
Collaborator

wjaykim commented Nov 20, 2021

@ShivamJoker Yes, I'll try that.

@wjaykim
Copy link
Collaborator

wjaykim commented Nov 20, 2021

Found that this issue comes from comparing options object with useEffect hook: https://stackoverflow.com/questions/54095994/react-useeffect-comparing-objects

Ended up using https://github.com/kentcdodds/use-deep-compare-effect internally. Changes will available in next version.

wjaykim added a commit that referenced this issue Nov 20, 2021
Fixes infinite render loop caused by comparing object with useEffect hook.

Change to use [use-deep-compare-effect](https://github.com/kentcdodds/use-deep-compare-effect) internally.

Refs: #54
@ShivamJoker
Copy link

Interesting findings :) Glad that you figured it

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

Successfully merging a pull request may close this issue.

3 participants