- Had a small eureka moment with React today. Declaring a plain ‘ol variable e.g
let
orconst
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 likeuseState
oruseRef
. What’s useful aboutuseRef
is that mutating it does not re-render. This is perfect for binding to something like amapboxGL
instance where the object is constantly changing.