How to avoid extra wrapping
in React?

前端 未结 10 985
孤独总比滥情好
孤独总比滥情好 2020-12-02 12:04

Today I have started learning ReactJS and after an hour faced with the problem.. I want to insert a component which has two rows inside a div on the page.A simplified exampl

10条回答
  •  自闭症患者
    2020-12-02 12:26

    This is still required, BUT React now make sure to create elements without creating an additional DOM element.

    The extra wrapping needed (normally with a parent div) because Reacts createElement method require a type parameter which is either a tag name string (such as 'div' or 'span'), a React component type (a class or a function). But this was before they introduce React Fragment.

    Refer this NEW api doc for createElement

    React.createElement : Create and return a new React element of the given type. The type argument can be either a tag name string (such as 'div' or 'span'), a React component type (a class or a function), or a React fragment type.

    here is the official example, Refer React.Fragment.

    render() {
      return (
        
          Some text.
          

    A heading

    ); }

提交回复
热议问题