Make useRef lazy — 4 ways

from blog Vladimir Klepov as a Coder, | ↗ original
I love useRef, but it lacks the lazy initializer functionality found in other hooks (useState / useReducer / useMemo). useRef({ x: 0, y: 0 }) creates an object { x: 0, y: 0 } on every render, but only uses it when mounting — it subsequent renders it's thrown away. With useState, we can replace the initial value with an initializer that's only...