- Had a small eureka moment with React today. Declaring a plain ‘ol variable e.g
let
or const
in a react component wont persist between re-renders. So if you initiate an instance of a class and bind it to a plain variable, the instance of the object will be lost if the component re-renders. To make sure the instance persists between re-renders we should use something like useState
or useRef
. What’s useful about useRef
is that mutating it does not re-render. This is perfect for binding to something like a mapboxGL
instance where the object is constantly changing.