ReactJs - Creating an “If” component… a good idea?

后端 未结 13 2359
南旧
南旧 2020-12-12 17:11

I\'ve read in the React docs that \"if\" type statements can\'t be used in JSX code, because of the way JSX renders into javascript, it doesn\'t work out as one would expect

13条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-12 17:31

    I think your main question is not "Can I implement an If component" (because clearly you can), but "Is there any reason why implementing an If component is a bad idea?"

    The main reason it's a bad idea is because an adds component render cycles to what would otherwise be a small amount of code in an existing component. The extra component wrapper means that React needs to run an entire extra render cycle on its children. This is not a big deal if the If component is only used in one place, but if your app is littered with them, you've just made your UI that much slower.

    Similar to how:

    Hi
    is more efficient than
    Hi
    .

提交回复
热议问题