2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Stateless functional componentsでnull返してはまった話

Last updated at Posted at 2015-10-20

React 0.14から入ったStateless functional components

どうもnullを返してしまうとRenderが無いと怒られてしまうらしい。

const FooItem = ({ show }) => {
  if(!show){
    return null
  }
  return (
    <div>some component</div>
  )
}

解決策としてはこんな感じ

const FooItem = ({ show }) => {
  if(!show){
    return <noscript />
  }
  return (
    <div>some component</div>
  )
}

<noscript>に深い意味があるわけでもないが、Reactの標準的な動作がこれらしいのであわせた。
spanでも多分別に良い。

Animation関連でもnull返した際にエラーがあったので、もしかするとnull返すComponentはそもそもあんまり良くないのかもしれない。

ちなみに、現象としてはissueに上がっていた。
https://github.com/facebook/react/issues/5218
でもshadowRenderに限定されてしまっている。

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?