2

In my hoc I have this conditional;

   branch(R.propSatisfies(R.isEmpty, "repos"), renderComponent(Loader)),
// branch(R.isEmpty("repos"), renderComponent(Loader)),

What's the difference and why the socond one gives me an error? test is not a function

Same result with this as well:

branch(R.isEmpty(R.props("repos")), renderComponent(Loader)),
karolis2017
  • 2,195
  • 8
  • 24
  • 49

2 Answers2

2

This is because R.propSatisfies has a different method signature to that of R.isEmpty.

In the case of your first approach:

branch(R.propSatisfies(R.isEmpty, "repos"), renderComponent(Loader))

the R.propSatisfies function is evaluating a function (R.isEmpty) on a property ("repos") of the input object (ie which is the object returned from renderComponent(Loader)).

In the case of your second approach:

// branch(R.isEmpty("repos"), renderComponent(Loader)),

what you're doing here is calling R.isEmpty directly. The R.isEmpty method expects an array, and returns true if the supplied array is empty. R.isEmpty doesn't have the ability to determine if a property (ie "repos") in an object is empty or not. To make it easier to visualise what's going on here, consider the following:

// Your second approach:
branch(R.isEmpty("repos"), renderComponent(Loader))

// ...which when expanded, is equivalent to this. You can now see it 
// shows incorrect usage of R.isEmpty
branch(component => R.isEmpty("repos")(component), renderComponent(Loader))

Hope this offers some clarification - for more information on R.isEmpty, see this link

Dacre Denny
  • 29,664
  • 5
  • 45
  • 65
  • Somehow I didn't see this one when I posted mine, with the same info. That's what I get for doing SO on my mobile. @karolis2017: I'm going to delete my repetitive answer. I'd suggest that you mark this one as the correct version. – Scott Sauyet Sep 05 '18 at 13:41
  • (I can't delete the accepted answer. But if you choose this one, I will try to do so later.) – Scott Sauyet Sep 05 '18 at 13:42
  • Hi @ScottSauyet - not a problem (and yes, SO + mobile isn't the easiest of combinations ;-)) – Dacre Denny Sep 05 '18 at 20:17
1

R. IsEmpty is unary function that reports whether a value is the empty value for its type (such as empty string, empty object, or empty array.). When you call it with "repos", you will get false since "repos" is not the empty string. Presumably the branch function you call wants a predicate function as the first argument and it fails when you send this boolean. Similarly, since R. props (you probably meant R.prop BTW, but the same issue would apply) is a binary function, R.props("repos") returns a function, which is not empty, so isEmpty returns false.

R.propSatisfies, on the other hand, is a ternary function accepting a predicate function, a property name, and an object. When you call it with isEmpty and "repos", you get back a function waiting for the object. That gets passed to branch and all is good.

Is there a reason you don't like the propSatisfies version?

Scott Sauyet
  • 49,207
  • 4
  • 49
  • 103