为什么这不适用于重构和ramda?

问题描述 投票:2回答:2

在我,我有条件;

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

有什么区别,为什么第二个给我一个错误? qazxsw poi

同样的结果:

test is not a function
reactjs ramda.js recompose
2个回答
1
投票

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.propSatisfiesisEmpty调用它时,你会得到一个等待对象的函数。这传递给"repos",一切都很好。

有没有理由你不喜欢branch版本?


2
投票

这是因为propSatisfiesR.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的更多信息

© www.soinside.com 2019 - 2024. All rights reserved.