我的React应用程序代码库中重复了以下模式:
const {items, loading} = this.props
const elem = loading
? <Spinner />
: items.length
? <ListComponent />
: <NoResults />
Run Code Online (Sandbox Code Playgroud)
尽管这肯定比嵌套实际 if/else子句更干净,但我正在尝试拥抱更优雅和实用的模式。我已经读过有关使用Eithermonad之类的东西的信息,但是我为之付出的所有努力最终看起来都更加冗长,并且可重用性较低(鉴于我想记住以前的尝试,该伪代码可能无法正常工作) :
import {either, F, isEmpty, prop} from 'ramda'
const isLoading = prop('loading')
const renderLoading = (props) => isLoading(props) ? <Spinner /> : false
const loadingOrOther = either(renderLoading, F)
const renderItems = (props) => isEmpty(props.items) ? <NoResults /> : <ListComponent />
const renderElem = either(loadingOrOther, renderItems)
const elems = renderElem(props)
Run Code Online (Sandbox Code Playgroud)
我可以使用哪种模式更干燥/可重用?
谢谢!