react-cursor
react-cursor copied to clipboard
Implement default values at a subtree
The problem is that, React state has getInitialState, so the default state can be defined inside the component. With cursors, the default state has to be declared somewhere else, at the top.
So suppose refine could take an optional extra argument, a fallback value
state = {a: {b: null }}
cursor.refine(['a', 'b']).value //=> null var not_found = {c: {d: 42}} c2 = cursor.refine(['a', 'b'], not_found); c3 = c2.refine(['c', 'd']); c3.value //> 42 c3.set(c3.value + 1); //> {a: {b: {c: {d: 43}}}}
I did implemented this in clojurescript and I found this helped clean up my components quite a bit.
The thoughtwork for this is done, we have a working clojure implementation. However, we will need to remove the varargs from refine - so the first argument needs to be a list. So this will break api compat and add some verbosity.
I want this for 2.0 but i don't want to break API compat, i may introduce a new API for this e.g. refineWithDefault
or refineWith
i have to think about the naming
Here's a compromise: we could do type-checking on the varargs - Unless the last arg to refine is a string, consider it a default value for the path given so far?
Of course, this would preclude strings from being default values, but it would preserve API compat. What other edge cases does this run into? I'm not super thrilled by the idea, but I'm just throwing it out there for your thoughts.
String default values are a very common case i think