Derived state for frontend applications comes up a lot. State
B depends on State
A, so after you update
A, by the time you read
B you want it to be updated based on the latest value of
A. There’s a bunch of ways to model this, involving different tradeoffs:
A popular solution is selector libraries like Reselect. The way they work is:
If you wrote in Scala or Haskell before you tried TypeScript, you may have found yourself wondering: Where are the Options at?
For those not familiar, here is an excellent introduction to Scala’s
Option type. At a high level,
Option is an abstraction over
null that gives useful semantics around running functions over possibly-null values. It implements a monad, a functor, and some other structures, but that’s not important for this post.