代码之家  ›  专栏  ›  技术社区  ›  karolis2017

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

  •  -1
  • karolis2017  · 技术社区  · 8 年前

    {!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 回复  |  直到 8 年前
        1
  •  0
  •   Scott Sauyet    8 年前

    我不太清楚你在找什么,但听起来你好像想要这样的东西:

    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 ?