useConstructor
useConstructor runs a constructor function once on the first render of a component and returns a value that will stay constant across the lifetime of the component.
Basic
We typically do our observable creation inside of useConstructor and then return the observables from the constructor for access during the remainder of the component's lifetime.
Unmounting
Whenever we observe atom's, we should take care to unobserve them when a component unmounts. For this, a beforeUnmount argument is passed to the constructor. Note that observe returns a dispose function so we can simply wrap our observe call with beforeUnmount and the dispose function will be invoked before unmount.
note
Queueing dispose functions ourselves may seem like uneccesary boilerplate, but it's in line with elementos' goals to be transparent and explicit and helps solidify our mental models of our apps. It also encourages us to decouple our state/effects from React's API, allowing for easier migrations to other frameworks.
Observing Props
Often times we wish to observe changes to props across renders. For this, useConstructor takes a second argument, observed, which should be an object whose values will be tracked as atoms and these atoms will be passed to the constructor function.