英文:
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 '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 other.
答案1
得分: 2
react-router-dom@5
的 Route
组件有 4 种渲染内容的方法。主要方法是直接将内容作为 children
属性进行渲染。请注意,此时不会传递路由属性给组件。
<Route path='/about'>
<About />
</Route>
另外三种方法是使用路由渲染方法之一。
使用
<Route>
渲染内容的推荐方法是使用子元素,如上所示。然而,还有一些其他方法可以用来渲染<Route>
中的内容。这些方法主要是为了支持在引入 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.
<Route path='/about'>
<About />
</Route>
The other 3 ways are to use one of the route render methods.
> The recommended method of rendering something with a <Route>
is to use
> children elements, as shown above. There are, however, a few other
> methods you can use to render something with a <Route>
. These are
> provided mostly for supporting apps that were built with earlier
> versions of the router before hooks were introduced.
>
> * <Route component>
> * <Route render>
> * <Route children>
function
Examples:
component
- Other common method, route props are implicitly passed as props to the component.
<Route path='/about' component={About} />
render
function - Alternative to pass along runtime props as well as the route props.
<Route path='/about' render={routeProps => <About {...routeProps} {...otherProps} />} />
children
function - Renders regardless of route match and passes route props to component to handle any conditional logic.
<Route path='/about' children={routeProps => <About {...routeProps} />} />
答案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 <Route>
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 = () => {
// call APIs, handle...
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>
</>
)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论