在我,我有条件;
branch(R.propSatisfies(R.isEmpty, "repos"), renderComponent(Loader)),
// branch(R.isEmpty("repos"), renderComponent(Loader)),
有什么区别,为什么第二个给我一个错误? qazxsw poi
同样的结果:
test is not a function
branch(R.isEmpty(R.props("repos")), renderComponent(Loader)),
是一元函数,用于报告值是否为其类型的空值(例如空字符串,空对象或空数组)。当你用R. IsEmpty
调用它时,你会得到"repos"
,因为false
不是空字符串。据推测,你调用的"repos"
函数需要一个谓词函数作为第一个参数,当你发送这个布尔值时它会失败。同样,由于branch
(你可能意味着R. props
BTW,但同样的问题将适用)是一个二元函数,R.prop
返回一个非空的函数,所以R.props("repos")
返回false。
另一方面,isEmpty
是一个接受谓词函数,属性名称和对象的三元函数。当你用R.propSatisfies
和isEmpty
调用它时,你会得到一个等待对象的函数。这传递给"repos"
,一切都很好。
有没有理由你不喜欢branch
版本?
这是因为propSatisfies
与R.propSatisfies
的方法签名不同。
在您的第一种方法的情况下:
R.isEmpty
branch(R.propSatisfies(R.isEmpty, "repos"), renderComponent(Loader))
函数正在评估输入对象的属性(R.propSatisfies
)上的函数(R.isEmpty
)(即从"repos"
返回的对象)。
在您的第二种方法的情况下:
renderComponent(Loader)
你在这里做的是直接调用// branch(R.isEmpty("repos"), renderComponent(Loader)),
。 R.isEmpty
方法需要一个数组,如果提供的数组为空,则返回true。 R.isEmpty
无法确定对象中的属性(即“repos”)是否为空。为了更容易想象这里发生了什么,请考虑以下事项:
R.isEmpty
希望这提供一些澄清 - 有关// 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))
,R.isEmpty
的更多信息