如何使用R.ifElse返回带有传递的道具的组件? [英] How do I return a component with passed props with R.ifElse?

查看:54
本文介绍了如何使用R.ifElse返回带有传递的道具的组件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究ramdajs并在此处重构一些反应代码.在这种情况下可能没有必要这样做,但是为了锻炼,我如何在R.ifElse中传递道具?

I'm diving into ramdajs and refactoring some react code here. It might not necessary to do it in this case but for the sake of exercise how do I pass props in R.ifElse ?

{!this.state.repos ? <Loader /> : <RepoGrid repos={this.state.repos} />}

// refactoring to:

{R.ifElse(
   R.isEmpty, 
   R.always(Loader), 
   RepoGrid
)(this.state.repos)}

这给我一个错误Cannot read property '@@transducer/step' of null

const ReposGrid = R.pipe(
    R.tap(console.log)
    R.prop("repos"),
    R.map(Repo),
    ReposWraper
)

推荐答案

我不太确定您要寻找的是什么,但这听起来好像您想要这样的东西:

I'm not quite sure what it is you're looking for, but it sounds as though you want something like this:

const ReposGrid = repos => `| ${repos.join(' | ')} |`;
const Loader = `Loader`

const transform = R.ifElse(
   R.isEmpty,
   R.always(Loader),
   ReposGrid
)

console.log(transform([])) 
//=> 'Loader'

console.log(transform(['foo', 'bar', 'baz']))
//=> '| foo | bar | baz |'

<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.js"></script>

提供给由ifElse返回的函数的参数被提供给条件函数,结果函数和替代函数.如果这不是您想要的,将身份传递给ReposGrid是什么意思?是否应该为其提供身份功能? (例如(x) => x?

The arguments supplied to the function returned by ifElse are supplied to the condition, consequent, and alternative functions. If this isn't what you want, what do you mean by passing identity to ReposGrid? Should it actually be supplied the identity function? (e.g. (x) => x?

这篇关于如何使用R.ifElse返回带有传递的道具的组件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆