reactjs - 为什么这不适用于 recompose 和 ramda?

标签 reactjs ramda.js recompose

我有这个条件;

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

有什么区别以及为什么第二个给我一个错误? 测试不是函数

与此相同的结果:

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

最佳答案

这是因为 R.propSatisfies 具有与 R.isEmpty 不同的方法签名。

对于您的第一种方法:

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

R.propSatisfies 函数正在对输入对象的属性 ("repos") 求值函数 (R.isEmpty) (即从 renderComponent(Loader) 返回的对象)。

对于第二种方法:

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

您在这里所做的是直接调用R.isEmpty。 R.isEmpty 方法需要一个数组,如果提供的数组为空,则返回 true。 R.isEmpty 无法确定对象中的属性(即“repos”)是否为空。为了更容易地直观地了解这里发生的情况,请考虑以下事项:

// 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 的更多信息,see this link

关于reactjs - 为什么这不适用于 recompose 和 ramda?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52175389/

相关文章:

reactjs - 如何在 AWS Amplify GraphQL 客户端中进行过滤

javascript - 重组 pure() 与 React.PureComponent

typescript :更改函数类型以使其返回新值

javascript - 使用 ramda 展平各种数组

javascript - 为什么要在标记点击事件上重新渲染 map ?

performance - 重新渲染列表的单行而不重新渲染整个列表

javascript - 带有 React 渲染 Prop 的流泛型

javascript - 将 Facebook Web SDK 与 ReactJS 组件状态集成

javascript - Ramda 的意外 'sortBy' 行为

javascript - 在 Ramda 中组合函数,每个函数接收相同的值