I'm trying to understand how to leverage immutability, and have a few questions:
-
The imperative code inupdateTodo
is the kind of stuff you can't guarantee people won't do, and is that the reason you have a very conservativeshouldComponentUpdate
by default? You say never to mutatethis.state
in the docs, but you assume that people are going to mutate objects within the app state? -
I'm having trouble figuring out how to writeshouldComponentUpdate
even if we updatethis.state
functionally. Won'tnextProps
always be new, sincerender
creates a new props object every time? How do we compareprops
andnextProps
? -
If a subcomponent has state (like the clock example that ticks every second), that state is isolated within the component and isn't attached to the top-level app state. How does Om compose state, while still keeping it in 1 data structure that lets us leverage immutability for fast dirty checking?
-
Relatedly, I don't understand why Om continually uses rAF if it always knows when the app state changes anyway - since with immutability you need to walk up and replace the top element, and something like
setState
will still be called, right?
(Yes, I could go read Om's source code and I am doing that, but it's good to hear answers from people too)
Om uses an abstraction called Cursors. Cursors are like a limited version of functional lenses, they provide a slice into the global application state - this preserves component modularity. Components only see what they need to and can update the global app state without having to know where that data actually resides. Cortex for React works via the same principle.
We don't use
setState
. Om continually uses rAF because it's just more efficient - all application state changes are batched.