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

后端 未结 13 2360
南旧
南旧 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条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-12 17:26

    I would try to avoid conditional rendering with "templates" altogether. In React and JSX you can use the full set of JavaScript functionality to make conditional rendering happen:

    • if-else
    • ternary
    • logical &&
    • switch case
    • enums

    That's what makes the rendering with JSX so powerful. Otherwise one could invent again own templating languages that are library/framework specific like in Angular 1/2.

提交回复
热议问题