React component closing tag

后端 未结 2 1335
无人共我
无人共我 2020-12-10 12:25

I\'m new to React and I\'m trying to figure out the purpose/use of vs . I can\'t seem to find information on any

2条回答
  •  佛祖请我去吃肉
    2020-12-10 12:58

    In React's JSX, you only need to write when the component has child components, like this:

    
        
        
        
    
    

    If there is nothing between and , then you can write it either or (but is generally preferred). Details in Introducing JSX.

    Just as a side note, you'd access those children in your component via the special props.children property. More in JSX in Depth: Children in JSX.

    Note that this is very much not like HTML or XHTML. It's its own (similar) thing with different rules. For instance, in HTML,

    is exactly the same thing as
    : A start tag, for which you must eventually have an end tag. Not so JSX (or XHTML). The rules for HTML are that void elements (elements that never have markup content, such as br or img) can be written with or without / before > and they never get an ending tag, but non-void elements (like div) must always have an ending tag (
    ), they cannot be self-closing. In JSX (and XHTML), they can be.

提交回复
热议问题