admin管理员组

文章数量:1327113

I'm having trouble creating a simple ponent that renders a menu in my react application.

This is the code for my ponent:

import React, { Component } from 'react';

export default class menu extends Component {

    render() {
        return (
            <nav className="navbar navbar-expand-lg navbar-light bg-light">
                <a className="navbar-brand" href="#">Navbar</a>
                <button className="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavDropdown" aria-controls="navbarNavDropdown" aria-expanded="false" aria-label="Toggle navigation">
                    <span className="navbar-toggler-icon"></span>
                </button>
                <div className="collapse navbar-collapse" id="navbarNavDropdown">
                    <ul className="navbar-nav">
                        <li className="nav-item active">
                            <a className="nav-link" href="#">Home <span className="sr-only">(current)</span></a>
                        </li>
                        <li className="nav-item">
                            <a className="nav-link" href="#">Features</a>
                        </li>
                        <li className="nav-item">
                            <a className="nav-link" href="#">Pricing</a>
                        </li>
                        <li className="nav-item dropdown">
                            <a className="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                                Dropdown link
                            </a>
                            <div className="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
                                <a className="dropdown-item" href="#">Action</a>
                                <a className="dropdown-item" href="#">Another action</a>
                                <a className="dropdown-item" href="#">Something else here</a>
                            </div>
                        </li>
                    </ul>
                </div>
            </nav>
        );
    }

}

I'm putting it in the other ponent.

import menu from './ponentes/menu';

constructor(props) { super(props) this.state = { sistemas: [] }; }

class App extends Component {

render() {
    return (
      <div>
        <menu></menu>
      </div>
      <div className="App">

        <div className="bs-header" id="content">
          <div className="container">
            <h1>Template Changelog</h1>
            <p>Lists all changes to the HTML template files</p>
          </div>
        </div>

); //[js] JSX expressions must have one parent element.
  }

}

I'm also getting the following error in the mented line

[js] JSX expressions must have one parent element.

I'm having trouble creating a simple ponent that renders a menu in my react application.

This is the code for my ponent:

import React, { Component } from 'react';

export default class menu extends Component {

    render() {
        return (
            <nav className="navbar navbar-expand-lg navbar-light bg-light">
                <a className="navbar-brand" href="#">Navbar</a>
                <button className="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavDropdown" aria-controls="navbarNavDropdown" aria-expanded="false" aria-label="Toggle navigation">
                    <span className="navbar-toggler-icon"></span>
                </button>
                <div className="collapse navbar-collapse" id="navbarNavDropdown">
                    <ul className="navbar-nav">
                        <li className="nav-item active">
                            <a className="nav-link" href="#">Home <span className="sr-only">(current)</span></a>
                        </li>
                        <li className="nav-item">
                            <a className="nav-link" href="#">Features</a>
                        </li>
                        <li className="nav-item">
                            <a className="nav-link" href="#">Pricing</a>
                        </li>
                        <li className="nav-item dropdown">
                            <a className="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                                Dropdown link
                            </a>
                            <div className="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
                                <a className="dropdown-item" href="#">Action</a>
                                <a className="dropdown-item" href="#">Another action</a>
                                <a className="dropdown-item" href="#">Something else here</a>
                            </div>
                        </li>
                    </ul>
                </div>
            </nav>
        );
    }

}

I'm putting it in the other ponent.

import menu from './ponentes/menu';

constructor(props) { super(props) this.state = { sistemas: [] }; }

class App extends Component {

render() {
    return (
      <div>
        <menu></menu>
      </div>
      <div className="App">

        <div className="bs-header" id="content">
          <div className="container">
            <h1>Template Changelog</h1>
            <p>Lists all changes to the HTML template files</p>
          </div>
        </div>

); //[js] JSX expressions must have one parent element.
  }

}

I'm also getting the following error in the mented line

[js] JSX expressions must have one parent element.

Share Improve this question asked Apr 10, 2018 at 0:34 Rafael AugustoRafael Augusto 7975 gold badges15 silver badges28 bronze badges 1
  • fyi you should capitalize menu. – Daniel A. White Commented Apr 10, 2018 at 0:46
Add a ment  | 

2 Answers 2

Reset to default 5

Your App has more than one top level div. Wrap both in another div.

In React 16, you can wrap your ponents with Fragment which is a cleaner approach as pared to wrap it with additional div tag.

All you have to do is just import Fragment from react library.

import React, { Component, Fragment } from 'react';

And you can use it

<Fragment>
  <ComponentA />
  <ComponentB />
</Fragment>

Documentation: https://reactjs/docs/fragments.html

Otherwise you can just wrap your ponents with div tag as mentioned by @Daniel A. White

本文标签: javascriptmust be wrapped in an enclosing tagStack Overflow