Skip to content
This repository has been archived by the owner on Oct 14, 2018. It is now read-only.

Implement default values at a subtree #38

Open
dustingetz opened this issue Apr 7, 2015 · 4 comments
Open

Implement default values at a subtree #38

dustingetz opened this issue Apr 7, 2015 · 4 comments
Milestone

Comments

@dustingetz
Copy link
Owner

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
Copy link
Owner Author

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 dustingetz added this to the 2.0 milestone Jan 16, 2016
@dustingetz
Copy link
Owner Author

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

@danielmiladinov
Copy link
Collaborator

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.

@dustingetz
Copy link
Owner Author

String default values are a very common case i think

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants