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

Infinite loop when used with react-native-web View #359

Closed
MoOx opened this issue Aug 8, 2020 · 6 comments
Closed

Infinite loop when used with react-native-web View #359

MoOx opened this issue Aug 8, 2020 · 6 comments
Assignees

Comments

@MoOx
Copy link

MoOx commented Aug 8, 2020

Everything is in the title. No issue with a div, as soon as I try to use RNW View, a infinite loop is started.

This is likely due to how RNW handle ref for the View (and maybe others)

https://github.com/necolas/react-native-web/blob/251cdfb220a165dc652bf118063139da2b9345f1/packages/react-native-web/src/exports/View/index.js#L104-L140

@thebuilder
Copy link
Owner

thebuilder commented Aug 8, 2020

Yeah, this is because they always trigger the ref callback function. The ref callback they create should be wrapped in a useCallback - Otherwise it will be executed on every render.

https://github.com/thebuilder/react-intersection-observer#how-can-i-assign-multiple-refs-to-a-component

@thebuilder
Copy link
Owner

I might also be able to modify the ref callback, so it doesn't recreate the observer if it's the same element and options.

@wildfrontend
Copy link

wildfrontend commented Oct 15, 2020

i find when i use Profiler inViewRef will cause Infinite loop

i use useInView to set ref

it is a bug?

sample code :https://codesandbox.io/s/serverless-cache-z7hdv?fontsize=14&hidenavigation=1&theme=dark

Oct-15-2020 13-35-48

@thebuilder
Copy link
Owner

thebuilder commented Oct 19, 2020

i find when i use Profiler inViewRef will cause Infinite loop

i use useInView to set ref

it is a bug?

sample code :codesandbox.io/s/serverless-cache-z7hdv?fontsize=14&hidenavigation=1&theme=dark

It's because you are creating a new threshold Array on each render, causing it to recreate the observer.

 threshold: [0.5]

Make sure to define the array outside the render function, or memorize it.

@thebuilder
Copy link
Owner

@wildfrontend I made a new release that should prevent that issue. #404

@thebuilder
Copy link
Owner

thebuilder commented Oct 19, 2020

@MoOx - In case you haven't noticed, this issue was resolved by react-native-web in necolas/react-native-web#1746

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

No branches or pull requests

3 participants