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

如果从呈现返回组件而不是<Component/>,则可能会发生这种情况

安奇
2023-03-14

我得到了这个错误函数是无效的反应子。如果您返回一个组件而不是从render返回,可能会发生这种情况。或者你想调用这个函数而不是返回它

位置“/”处的匹配叶路由没有元素。这意味着默认情况下,它将呈现一个具有空值的,从而产生一个“空”页。

这一个

应用程序。js文件是

import React, { Component } from 'react'; 
import HomePage from './pages/HomePage'; 
import { BrowserRouter, Routes, Route, } from 'react-router-dom'; 
import './App.css'; 
import { render } from '@testing-library/react';

class App extends Component {   render() {
    return (
      <BrowserRouter>
      <Routes>
          <Route path='/' element={HomePage} exact/>
      </Routes>
      </BrowserRouter>
    );   } }

export default App;

和Homepage.js

import React from 'react';

const HomePage = () => (
    <>
    <h1>Hello, welcome to my blog!</h1>
    <p>
        Welcome to my blog! Proin congue
        ligula id risus posuere, vel eleifend ex egestas. Sed in turpis leo. 
        Aliquam malesuada in massa tincidunt egestas. Nam consectetur varius turpis, 
        non porta arcu porttitor non. In tincidunt vulputate nulla quis egestas. Ut 
        eleifend ut ipsum non fringilla. Praesent imperdiet nulla nec est luctus, at 
        sodales purus euismod.
    </p>
    <p>
        Donec vel mauris lectus. Etiam nec lectus urna. Sed sodales ultrices dapibus. 
        Nam blandit tristique risus, eget accumsan nisl interdum eu. Aenean ac accumsan 
        nisi. Nunc vel pulvinar diam. Nam eleifend egestas viverra. Donec finibus lectus 
        sed lorem ultricies, eget ornare leo luctus. Morbi vehicula, nulla eu tempor 
        interdum, nibh elit congue tellus, ac vulputate urna lorem nec nisi. Morbi id 
        consequat quam. Vivamus accumsan dui in facilisis aliquet.,
    </p>
    <p>
        Etiam nec lectus urna. Sed sodales ultrices dapibus. 
        Nam blandit tristique risus, eget accumsan nisl interdum eu. Aenean ac accumsan 
        nisi. Nunc vel pulvinar diam. Nam eleifend egestas viverra. Donec finibus lectus 
        sed lorem ultricies, eget ornare leo luctus. Morbi vehicula, nulla eu tempor 
        interdum, nibh elit congue tellus, ac vulputate urna lorem nec nisi. Morbi id 
        consequat quam. Vivamus accumsan dui in facilisis aliquet.,
    </p>
    </>
);

export default HomePage;

共有1个答案

慕容文昌
2023-03-14

替换<code>

您需要将其作为元素而不是道具传递,因为响应路由器v6。

 类似资料: