简体   繁体   English

使用 React Router Dom 时出错

[英]Getting error while using React Router Dom

I'm new to React, trying to learn and play around with react router, I'm getting following warnings, error while trying to navigate:我是 React 新手,正在尝试学习和使用 React 路由器,但在尝试导航时收到以下警告和错误:

Warning: React.jsx: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: .警告:React.jsx: type is invalid -- 期望一个字符串(对于内置组件)或一个类/函数(对于复合组件)但是得到:. Did you accidentally export a JSX literal instead of a component?您是否不小心导出了 JSX 文字而不是组件?

Though I never used Fragment anywhere in my code.尽管我从未在代码中的任何地方使用过 Fragment。

When I click on "About Section" to navigate, I get the following error:当我单击“关于部分”进行导航时,出现以下错误:

Uncaught Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object.未捕获的错误:元素类型无效:应为字符串(对于内置组件)或类/函数(对于复合组件),但得到:对象。 Check the render method of App .检查App的渲染方法

Can someone please explain me, what am I doing wrong?有人可以解释一下,我做错了什么吗? Why is that warning coming up in console?为什么控制台中会出现该警告? Maybe a little more insight about how things work in background would be helpful.也许对事情在后台如何工作有更多的了解会有所帮助。 Thanks in advance.提前致谢。

Here's my code ->这是我的代码->

Index.js -索引.js -

import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './App';
import './index.css';
import reportWebVitals from './reportWebVitals';

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
    <App />
);

reportWebVitals();

App.js -应用程序.js -

import { BrowserRouter, Route, Routes } from 'react-router-dom';
import './App.css';
import About from './components/About';
import Main from './components/Main';

function App() {
  const styleObjectApp = {
    backgroundColor: 'pink',
    border: 'solid 1px black',
    padding: '2%',
    margin: '2%',
  };
  return (
    <>
      <BrowserRouter>
        <Routes>
          <Route path='/' element={<Main />} />
          <Route path='about' element={<About />} />
        </Routes>
      </BrowserRouter>
      <div style={styleObjectApp}>
        This is App component!
      </div>
    </>
  );
}

export default App;

Main.js - Main.js -

import { Link, Outlet } from "react-router-dom";

function Main() {
    const styleObject = {
        border: 'solid 1px black',
        backgroundColor: 'lime',
        padding: '2%',
        margin: '2%',
    }
    return (
        <>
            <div style={styleObject}>
                This is Main Container Component!!
                <div style={{ float: 'right' }}>
                    <Link to='/about'>About Section</Link>
                </div>
            </div>
            <Outlet/>
        </>
    )
}

export default Main;

About.js -关于.js -

function About() {
    return (
        <>
            This is About Component!!
        </>
    )
}

export default About();

Hello please export a component like this您好,请导出这样的组件

function About() {
    return (
        <>
            This is About Component!!
        </>
    )
}

export default About;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM