react< route> route/> react< route/gt; react< react> exement属性和组件属性之间有什么区别标签

发布于 2025-02-01 13:41:54 字数 495 浏览 1 评论 0 原文

案例1:在路由标签

const App = () => {
  return(
    <Routes>
      <Route path="/" element={<Home />} />
      <Route path="/album" element={<Album />} />
    </Routes>
  )
};

案例2中使用元素属性时:在路由中使用组件属性时

const App = () => {
  return(
    <Routes>
      <Route path="/" component={<Home />} />
      <Route path="/album" component={<Album />} />
    </Routes>
  )
};

Case 1: when using element attribute within Route tag

const App = () => {
  return(
    <Routes>
      <Route path="/" element={<Home />} />
      <Route path="/album" element={<Album />} />
    </Routes>
  )
};

Case 2: when using component attribute within Route tag

const App = () => {
  return(
    <Routes>
      <Route path="/" component={<Home />} />
      <Route path="/album" component={<Album />} />
    </Routes>
  )
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

诺曦 2025-02-08 13:41:54

React-Router的当前文档说,所有选项基本上都在做同样的事情,但是它们会留下它们以支持支持旧版本,但是您只能使用其中之一。

The current documentation of react-router says all there options basically does the same thing, but they leave them in order to Support older versions, but you can only use one of those.

react-router-documentation

戴着白色围巾的女孩 2025-02-08 13:41:54

您可能正在使用等于或大于v5.1 的版本,

根据迁移文档,您可以在任何地方安全地使用元素

“如果您首先升级到v5.1。在v5.1中,我们对v5.1进行了反应路由器v6会更容易。我们对处理元素的处理释放了增强功能,这些元素将有助于平滑过渡到v6。而不是使用和道具,只需在各处使用常规元素即可使用钩子访问路由器的内部状态。

You are probably using a version equal to or greater than v5.1

According to migration documentation, you can use element safely everywhere.

"It will be easier to make the switch to React Router v6 if you upgrade to v5.1 first. In v5.1, we released an enhancement to the handling of elements that will help smooth the transition to v6. Instead of using and props, just use regular element everywhere and use hooks to access the router's internal state."

逆流 2025-02-08 13:41:54

情况1:在路由标签中使用元素属性时,您必须在tag.ex中指定组件名称:( element = {&lt; .../&gt;})。

const App = () => {
  return(
    <Routes>
      <Route path="/" element={<Home />} />
      <Route path="/album" element={<Album />} />
    </Routes>
  )
};

情况2:在路由标签中使用组件属性时,您可以在卷曲括号中写下组件名称,而不需要添加tag.ex.ex :( component = {...})。

const App = () => {
  return(
    <Routes>
      <Route path="/" component={Home} />
      <Route path="/album" component={Album} />
    </Routes>
  )
};

Case 1: when using element attribute within Route tag, you have to specify your component name within tag.ex:(element={<.../>}).

const App = () => {
  return(
    <Routes>
      <Route path="/" element={<Home />} />
      <Route path="/album" element={<Album />} />
    </Routes>
  )
};

Case 2: when using component attribute within Route tag, you can simply write your component name in curly braces doesn't need to add tag.ex:(component={...}).

const App = () => {
  return(
    <Routes>
      <Route path="/" component={Home} />
      <Route path="/album" component={Album} />
    </Routes>
  )
};
萧瑟寒风 2025-02-08 13:41:54

元素和组件属性之间的主要衍射;
首先:如果您升级到v5.1,则更容易地进行反应路由器V6。
第二:元素属性给出了使用道具的oppurtunity,但是在组件属性中,您不能使用。

<Routes>
 <Route path='/home' element={<Home prop={"somedata"}/>}/>
 <Route path='/about' Component={About} >
</Routes>

The main diffrences between element and component attributes;
First:It will be easier to make the switch to React Router v6 if you upgrade to v5.1
Second:element attribute gives oppurtunity to use props but in component attribute you cant.

<Routes>
 <Route path='/home' element={<Home prop={"somedata"}/>}/>
 <Route path='/about' Component={About} >
</Routes>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文