How do I return a component with passed props with R.ifElse?

梦想与她 提交于 2019-12-13 08:41:18

问题


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)}

This gives me an error Cannot read property '@@transducer/step' of null

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

回答1:


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>

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?



来源:https://stackoverflow.com/questions/51996525/how-do-i-return-a-component-with-passed-props-with-r-ifelse

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!