ericclemmons / react-resolver

Async rendering & data-fetching for universal React applications.
https://ericclemmons.github.io/react-resolver
Other
1.65k stars 52 forks source link

feature request: optional param to pass logic to react-resolver to determine if it should resolve or not? #86

Open babsonmatt opened 9 years ago

babsonmatt commented 9 years ago

Any thoughts on allowing an optional param to be passed to react-resolver to specify when it should actually resolve? I have a situation where just checking whether or not props has a certain key isn't enough and it'd really come in handy to be able to have some custom logic there.

Thanks!

ericclemmons commented 9 years ago

Hah! It sounds like #84 may help. The resolver will bypass any prop that already exists (since it doesn't need resolving).

Can you provide an example?

What I wanted to do in the past was actually having the resolver look at React.PropTypes.foo and checking for the isRequired. If it's required, it always resolves. If it's optional, it would not.

babsonmatt commented 9 years ago

I use reselect along with redux, and the issue I'm having is the props returned from reselect/redux may have the key for the object I'm trying to load, but the value of it may be something like an empty object, or null, etc. which means react-resolver won't try to load anything... right? So I'd like to be able to specify a custom function to run to determine if something should be loaded or not, ie: obj.length == 0, etc and not just whether or not the key exists in props.

ericclemmons commented 9 years ago

Ah interesting!

I did consider changing this behavior to do typeof value === "undefined".

https://github.com/ericclemmons/react-resolver/blob/1cf78025f1d3a17f23ce1ad86f0b657a508c122c/src/Resolver.js#L149

Would that solve it for you in the interim? The logic is, if the key is undefined, then it should be resolved.

However, null is a valid value for resolution. Similarly, an empty object {} is potentially valid too.

I'm down for coming up with a solution. I just didn't know if undefined can be set on fields that are...well, undefined :D