react-cursor icon indicating copy to clipboard operation
react-cursor copied to clipboard

Implement default values at a subtree

Open dustingetz opened this issue 9 years ago • 4 comments

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.

dustingetz avatar Apr 07 '15 17:04 dustingetz

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.

dustingetz avatar Dec 09 '15 16:12 dustingetz

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

dustingetz avatar Jan 16 '16 15:01 dustingetz

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.

danielmiladinov avatar Feb 14 '16 15:02 danielmiladinov

String default values are a very common case i think

dustingetz avatar Feb 14 '16 16:02 dustingetz