Simplify the observer example

This commit is contained in:
Dan Abramov
2019-05-06 03:05:28 +01:00
committed by GitHub
parent b1691780aa
commit ad6687e25e

View File

@@ -809,13 +809,10 @@ function Image(props) {
// ✅ IntersectionObserver is created lazily once
function getObserver() {
let observer = ref.current;
if (observer !== null) {
return observer;
if (ref.current === null) {
ref.current = new IntersectionObserver(onIntersect);
}
let newObserver = new IntersectionObserver(onIntersect);
ref.current = newObserver;
return newObserver;
return ref.current;
}
// When you need it, call getObserver()