当前位置: 首页 > 知识库问答 >
问题:

ReactApp:错误:元素类型无效:应为字符串(对于内置组件)或类/函数(对于复合组件),但获得:object[重复]

通迪
2023-03-14

我得到这个错误:

未捕获错误:不变冲突:元素类型无效:需要字符串(对于内置组件)或类/函数(对于复合组件),但得到:对象检查PageWrapper的呈现方法。

这是我的应用程序。js

import React from 'react';
import PageWrapper from "./components/PageWrapper";
import Home from "./components/Pages/Home.js";
import {BrowserRouter as Router,Route,Link} from 'react-router-dom';
function App() {
  return (
    <div>
    
    <Router>
      <PageWrapper>
          
            <Route path="/"
            component={Home}
            ></Route>
        
        </PageWrapper>
      </Router>
      </div>
  );
}

export default App;

这是我的页面包装:

import React from 'react';
import Link from 'react';

class PageWrapper extends React.Component{

        render(){

            return(
               <div>
                <nav className="navbar navbar-expand-lg navbar-dark fixed-top" id="mainNav">
                    <div className="container">
                        <Link className="navbar-brand js-scroll-trigger" to="/"><img src="assets/img/navbar-logo.svg" alt="" /></Link>
                        <button className="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation">
                            Menu
                            <i className="fas fa-bars ml-1"></i>
                        </button>
                        <div className="collapse navbar-collapse" id="navbarResponsive">
                            <ul className="navbar-nav text-uppercase ml-auto">
                                <li className="nav-item"><Link className="nav-link js-scroll-trigger" to="/services">Services</Link></li>
                                <li className="nav-item"><Link className="nav-link js-scroll-trigger" to="/portfolio">Portfolio</Link></li>
                                <li className="nav-item"><Link className="nav-link js-scroll-trigger" to="/about">About</Link></li>
                                <li className="nav-item"><Link className="nav-link js-scroll-trigger" to="/team">Team</Link></li>
                                <li className="nav-item"><Link className="nav-link js-scroll-trigger" to="/contact">Contact</Link></li>
                            </ul>
                        </div>
                    </div>
                </nav>
                {this.props.children}
               </div> 
                );
        }
}


export default PageWrapper;

请帮忙!

共有1个答案

周宏伯
2023-03-14

你的问题是

import Link from 'react';

PageWrapper组件

它应该是一个命名的导入,它应该来自react-router-dom而不是React

所以把它改成

import {Link} from 'react-router-dom';

它会工作得很好。

 类似资料: