如何使用R.ifElse返回带有传递的道具的组件?

时间:2018-08-24 02:06:08

标签: reactjs functional-programming ramda.js

我正在研究ramdajs并在此处重构一些反应代码。在这种情况下,可能不需要这样做,但是为了锻炼,我如何在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
)

1 个答案:

答案 0 :(得分:0)

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

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