<Route /> vs <Route> </Route> 在React中有什么区别?

huangapple go评论57阅读模式
英文:

What is the difference between <Route /> vs <Route> </Route> in React?

问题

I am using the react-router-dom and creating some routes in my application. Can anyone please explain me the specific usages of <Route /> and <Route></Route>. What will be the difference affect in the rendering if there are any. I will include a sample code snippet.

import { BrowserRouter as Router, Route } from 'react-router-dom';

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
  <Router>
    <Route path="/home" component={App} />

    <Route path='/about'>
      <About />
    </Route>
  </Router>
);

I tried both and both are working fine. But I was unable to find the specific usage of one over the other.

英文:

I am using the react-router-dom and creating some routes in my application. Can anyone please explain me the specific usages of <Route /> and <Route> </Route>. What will be the difference affect in the rendering if there are any. I will include a sample code snippet.

import { BrowserRouter as Router, Route } from &#39;react-router-dom&#39;;

const root = ReactDOM.createRoot(document.getElementById(&#39;root&#39;));
root.render(
  &lt;Router&gt;
    &lt;Route path=&quot;/home&quot; component={App} /&gt;

    &lt;Route path=&#39;/about&#39;&gt;
      &lt;About /&gt;
    &lt;/Route&gt;
    
  &lt;/Router&gt;
);

I tried both and both are working fine. But I was unable to find the specific usage of one over other.

答案1

得分: 2

react-router-dom@5Route 组件有 4 种渲染内容的方法。主要方法是直接将内容作为 children 属性进行渲染。请注意,此时不会传递路由属性给组件。

<Route path='/about'>
  <About />
</Route>

另外三种方法是使用路由渲染方法之一。

使用 &lt;Route&gt; 渲染内容的推荐方法是使用子元素,如上所示。然而,还有一些其他方法可以用来渲染 &lt;Route&gt; 中的内容。这些方法主要是为了支持在引入 hooks 之前构建的应用程序。

  • <Route component> - 其他常见方法,路由属性会隐式传递给组件作为属性。
<Route path='/about' component={About} />
  • <Route render> - 用于传递运行时属性以及路由属性的替代方法。
<Route path='/about' render={routeProps => <About {...routeProps} {...otherProps} />} />
  • <Route children> 函数 - 无论路由匹配与否都进行渲染,并将路由属性传递给组件以处理任何条件逻辑。
<Route path='/about' children={routeProps => <About {...routeProps} />} />
英文:

The react-router-dom@5 Route component has 4 ways to render content. The primary method is to directly render content as the children prop. Note here that no route props will be passed to the component.

&lt;Route path=&#39;/about&#39;&gt;
  &lt;About /&gt;
&lt;/Route&gt;

The other 3 ways are to use one of the route render methods.

> The recommended method of rendering something with a &lt;Route&gt; is to use
> children elements, as shown above. There are, however, a few other
> methods you can use to render something with a &lt;Route&gt;. These are
> provided mostly for supporting apps that were built with earlier
> versions of the router before hooks were introduced.
>
> * &lt;Route component&gt;
> * &lt;Route render&gt;
> * &lt;Route children&gt; function

Examples:

component - Other common method, route props are implicitly passed as props to the component.

&lt;Route path=&#39;/about&#39; component={About} /&gt;

render function - Alternative to pass along runtime props as well as the route props.

&lt;Route path=&#39;/about&#39; render={routeProps =&gt; &lt;About {...routeProps} {...otherProps} /&gt;} /&gt;

children function - Renders regardless of route match and passes route props to component to handle any conditional logic.

&lt;Route path=&#39;/about&#39; children={routeProps =&gt; &lt;About {...routeProps} /&gt;} /&gt;

答案2

得分: 1

只是对 Mr. Drew Reese 的回答 的一个小补充,直接在 <Route> 内呈现组件的方式允许您自由地传递自己的 props 到组件中(这更类似于我们通常使用普通的 React 组件的方式)。

例如:您有一个关于页面,其中包括 3 个选项卡:“关于我”,“关于我的团队”,“关于我的博客”。

const About = () => {
  // 调用 API,处理...

  return (
    <>
      <Switch>
        <Route path='/about-me'>
          <Me data={dataMe} />
        </Route>
        <Route path='/about-team'>
          <Team data={dataTeam} />
        </Route>
        <Route path='/about-blog'>
          <Blog data={dataBlog} />
        </Route>
      </Switch>
    </>
  )
}
英文:

Just a small add to Mr. Drew Reese's answer, the way rendering a component directly inside &lt;Route&gt; allow you to freely pass your own props inside your component (it's more familiar to the way we usually do with normal React components).

Ex: You have a page About, inside, it includes 3 tabs: "About me", "About my team", "About my blog".

const About = () =&gt; {
  // call APIs, handle...

  return (
    &lt;&gt;
      &lt;Switch&gt;
        &lt;Route path=&#39;/about-me&#39;&gt;
          &lt;Me data={dataMe} /&gt;
        &lt;/Route&gt;
        &lt;Route path=&#39;/about-team&#39;&gt;
          &lt;Team data={dataTeam} /&gt;
        &lt;/Route&gt;
        &lt;Route path=&#39;/about-blog&#39;&gt;
          &lt;Blog data={dataBlog} /&gt;
        &lt;/Route&gt;
      &lt;/Switch&gt;
    &lt;/&gt;
  )
}

huangapple
  • 本文由 发表于 2023年2月6日 15:51:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/75358598.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定