admin管理员组文章数量:1305714
I am refactoring a stateless functional ponent to use branch
and renderComponent
from repose
.
The original ponent looks like this:
const Icon = props => {
const { type, name } = props
let res
if (type === 'font') {
return (<FontIcon name={name} />)
} else if (type === 'svg') {
res = (<SvgIcon glyph={name} />)
}
return res
}
The ponent with branch looks like this:
const isFont = props => {
const { type } = props
return type === 'font'
}
const FontIconHoC = renderComponent(FontIcon)
const SvgIconHoC = renderComponent(SvgIcon)
const Icon = branch(isFont, FontIconHoC, SvgIconHoC)
Icon.propTypes = {
type: string,
name: string
}
export default Icon
I try and render the ponent using:
<Icon name='crosshairs' type='font' />
The resulting error looks like this:
invariant.js:44Uncaught Error: Icon(...): A valid React element (or null) must be returned. You may have returned undefined, an array or some other invalid object.
I am refactoring a stateless functional ponent to use branch
and renderComponent
from repose
.
The original ponent looks like this:
const Icon = props => {
const { type, name } = props
let res
if (type === 'font') {
return (<FontIcon name={name} />)
} else if (type === 'svg') {
res = (<SvgIcon glyph={name} />)
}
return res
}
The ponent with branch looks like this:
const isFont = props => {
const { type } = props
return type === 'font'
}
const FontIconHoC = renderComponent(FontIcon)
const SvgIconHoC = renderComponent(SvgIcon)
const Icon = branch(isFont, FontIconHoC, SvgIconHoC)
Icon.propTypes = {
type: string,
name: string
}
export default Icon
I try and render the ponent using:
<Icon name='crosshairs' type='font' />
The resulting error looks like this:
invariant.js:44Uncaught Error: Icon(...): A valid React element (or null) must be returned. You may have returned undefined, an array or some other invalid object.
Share
Improve this question
edited Mar 16, 2017 at 20:16
vamsiampolu
asked Mar 16, 2017 at 20:11
vamsiampoluvamsiampolu
6,64220 gold badges89 silver badges193 bronze badges
2 Answers
Reset to default 13branch
returns a HOC, which accepts a ponent and return a ponent, so branch(...)
is a HOC and branch(...)(...)
is a ponent.
In your case, because Icon
is not a ponent but a HOC, so React can't render it. To fix it, you can move SvgIcon
out from branch
's arguments and apply it to the HOC returned by branch(...)
, ex:
const Icon = branch(
isFont,
FontIconHoC,
a => a
)(SvgIcon)
We apply an identity function (a => a
) to the third argument of branch
. You can think of the identity function is also a HOC, which basically just return the ponent it gets and does nothing more.
Because this pattern is used very often, so the third argument of branch
is default to the identity function. As a result, we can omit it and make our code simpler:
const Icon = branch(
isFont,
FontIconHoC
)(SvgIcon)
I've created a jsfiddle for these code. You can try it here.
You can also just use an if statement instead of branch. Consider that you just had some difficulties doing what an if statement does.
Maybe time to reconsider that library ?
本文标签: javascriptUsing branch from recomposeStack Overflow
版权声明:本文标题:javascript - Using branch from recompose - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741810743a2398777.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论