$chiubaca / notes / fleeting / 2021-05-15
  • 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.
Edit on GitHub ✏️

Hey I'm Alex Chiu 👋. These are my notes for literally anything.

I'm using the Zettelkasten method to organise my thoughts here.

My more polished writings are published to my main blog at chiubaca.com

Follow me on: Github / X (Twitter) / Mastodon / LinkedIn