-
Notifications
You must be signed in to change notification settings - Fork 7.8k
Description
New docs on avoiding recreating ref contents has two examples:
const playerRef = useRef(new VideoPlayer());
const playerRef = useRef(null);
if (playerRef.current === null) {
playerRef.current = new VideoPlayer();
}
Even the old docs have similar examples:
const ref = useRef(new IntersectionObserver(onIntersect));
const ref = useRef(null);
if (ref.current === null) {
ref.current = new IntersectionObserver(onIntersect);
}
Both new VideoPlayer()
and new IntersectionObserver(onIntersect)
look like calls that might contain side effects inside.
My question is it allowed in any of the above examples, for these calls to contain side effects?
-
imho in Examples 1/3 they should not because they are executed on each render; although only values from initial render are kept.
-
But examples 2/4 suggest a pattern which make reading and writing to refs ok - which normally is an impure operation already. So I thought maybe in those examples at least, it is ok for
new VideoPlayer()
andnew IntersectionObserver(onIntersect)
to contain side effects?
It would be helpful if the docs clarified this.