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
Add a ment  | 

2 Answers 2

Reset to default 13

branch 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