首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >节不是<Route>组件。<Routes>的所有组件子组件都必须是<Route>

节不是<Route>组件。<Routes>的所有组件子组件都必须是<Route>
EN

Stack Overflow用户
提问于 2022-07-24 12:58:05
回答 2查看 475关注 0票数 1

注册和登录组件需要添加到容器类中。我对乌德米上了个反应课。他们正在使用一个较旧版本的反应-路由器-多姆。为此,我使用了v6反应路由器域并进行了更改,但这一次我不知道该做什么。这个代码对我来说是新的,请帮助我。

代码语言:javascript
复制
function App() {
  return (
    <Router>
      <Fragment>
        <Navbar />
        <Routes>
          <Route exact path='/' element={<Landing />} />
          <section className='container'>
            <Route exact path='/register' element={Register} />
            <Route exact path='/login' element={Login} />
          </section>
        </Routes>
      </Fragment>
    </Router>
  );
}

控制台中的错误

代码语言:javascript
复制
[section] is not a <Route> component. All component children of <Routes> must be a <Route>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-07-24 19:22:23

当错误通知您时,只有RouteReact.FragmentRoutes组件的有效子组件。

如果希望将多个路由组件呈现到特定的布局中,即公共UI/布局,则创建一个要嵌套的布局路由。

确保还将RegisterLogin呈现为JSX!

示例:

代码语言:javascript
复制
import { Outlet } from 'react-router-dom';

const SectionLayout = () => (
  <section className='container'>
    <Outlet /> // <-- nested routes render content here
  </section>
);

export default SectionLayout;

..。

代码语言:javascript
复制
import SectionLayout from '../path/to/SectionLayout';

...

<Routes>
  <Route path='/' element={<Landing />} />
  <Route element={<SectionLayout />}>
    <Route path='/register' element={<Register />} />
    <Route path='/login' element={<Login />} />
  </Route>
</Routes>

有关更多信息,请参见:

票数 2
EN

Stack Overflow用户

发布于 2022-07-24 13:38:00

我认为这个错误本身是相当有描述性的。<Routes />的子类只能是<Route />,而<section />不能满足这一要求。

如果您同时需要RegisterLogin组件,可以使用.container类包装section

我们可以通过不同的方法来实现这一目标,以下是其中的几个。

例如:

代码语言:javascript
复制
/**
* 1. Putting them inside the components itself
*/

const Register = () => {
 return (
  <section className="container">
    // your other codes here
  </section>
 )
}

const Login = () => {
 return (
  <section className="container">
    // your other codes here
  </section>
 )
}

/**
* 2. As a reusable Layout wrapper or Higher Order Component or 
* Useful when you have many shared contents and styling 
*/

const Container = (props) => {
 return (
  <section className="container">
   // shared contents
   {props.children}
   // other shared contents
  </section>
 );
}

const Register = () => {
 return (
  <Container>
    // your other codes here
  </Container>
 )
}

const Login = () => {
 return (
  <Container>
    // your other codes here
  </Container>
 )
}

希望这能有所帮助。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73098607

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档