为什么我总是收到带有React Router的错误消息

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了为什么我总是收到带有React Router的错误消息相关的知识,希望对你有一定的参考价值。

每次运行npm start都会给我一条错误消息:

错误:元素类型无效:需要一个字符串(对于内置组件)或一个类/函数(对于复合组件),但得到了:对象。您可能忘记了从定义文件中导出组件,或者可能混淆了默认导入和命名导入

import React,  Component  from "react";
import 
    Route,
    NavLink,
    HashRouter
   from "react-router-dom";
  import Home from "./containers/Home";
  import About from "./containers/About";
  import Resume from "./containers/Resume";
 
class Main extends Component 
  render() 
    return (
        <HashRouter>
        <div>
          <h1>Simple SPA</h1>
          <ul className="header">
            <li><NavLink to="/">Home</NavLink></li>
            <li><NavLink to="/about">About</NavLink></li>
            <li><NavLink to="/resume">Resume</NavLink></li>
          </ul>
          <div className="content">
            <Route path="/" component=Home/>
            <Route path="/about" component=About/>
            <Route path="/resume" component=Resume/>
             
          </div>
        </div>
        </HashRouter>
    );
  

 
export default Main;

import React from "react";
import ReactDOM from "react-dom";
import Main from "./Main";
 
ReactDOM.render(
  <Main/>, 
  document.getElementById("root")
);

import React,  Component  from "react";
 
class Home extends Component 
  render() 
    return (
      <div>
        <h2>HELLO</h2>
      </div>
    );
  

 
export default Home;
答案

嗨,在这里工作正常。请检查以下示例:

import React, useState from 'react';
import BrowserRouter as Router, HashRouter, Link, NavLink, Route, Switch from "react-router-dom";

class App extends React.Component 
    render() 
        return (
            <HashRouter>
                <div>
                    <h1>Simple SPA</h1>
                    <ul className="header">
                        <li><NavLink to="/">Home</NavLink></li>
                        <li><NavLink to="/about">About</NavLink></li>
                        <li><NavLink to="/resume">Resume</NavLink></li>
                    </ul>
                    <div className="content">
                        <Route path="/" component=Home/>
                        <Route path="/about" component=About/>
                        <Route path="/resume" component=Resume/>
                    </div>
                </div>
            </HashRouter>
        )
    

export default App;

class Home extends React.Component 
  render() 
    return (
      <div>
        <h2>HELLO</h2>
      </div>
    );
  


class About extends React.Component 
  render() 
    return (
      <div>
        <h2>About</h2>
      </div>
    );
  


class Resume extends React.Component 
  render() 
    return (
      <div>
        <h2>Resume</h2>
      </div>
    );
  

以上是关于为什么我总是收到带有React Router的错误消息的主要内容,如果未能解决你的问题,请参考以下文章

./src/App.js 尝试导入错误:“Switch”未从“react-router-dom”导出

Root Navlink 总是获得活动类 React Router Dom

使用带有打字稿的 react-router

从 v3 迁移到 v4 react-router 时出现错误

我收到“错误:React.Children.only 预计会收到一个 React 元素子项。”带有 TouchableWithoutFeedback

使用带有 Flux 的 React Router 以编程方式重定向