简体   繁体   English

在 React 中使用 react-router-dom 在 App.js 中配置路由的更好方法是什么

[英]What's the better way to configure routes in App.js in React with react-router-dom

In App.js of React, we can either:在 React 的 App.js 中,我们可以:

<div className="App">
  <Router>
    <Switch>
      <Route
        path="/basics"
        >
        <BasicsPage></BasicsPage>
      </Route>
      // other routes
    </Switch>
  </Router>
</div>

OR或者

<div className="App">
  <Router>
    <Switch>
      <Route
        path="/basics"
        component={BasicsPage}
        >
      </Route>
      // other routes
    </Switch>
  </Router>
</div>

Just wondering which is the better way to write it and if there' any difference between the two?只是想知道哪种写法更好,两者之间是否有任何区别? Thanks!谢谢!

The second solution is the best mainly because it provide matches props to the component.第二种解决方案是最好的,主要是因为它为组件提供了matches道具。

Here is a codesandbox exemple. 这是一个代码沙盒示例。 (check the index.js file than contain routes) (检查index.js文件而不是包含路由)

But each solution is usefull.但是每个解决方案都是有用的。

If you want pass props to the BasicPage the first way is better to add all of props you want.如果您想将道具传递给BasicPage ,第一种方法最好添加您想要的所有道具。

But if you have the choice, use the second solution which is more maintainable and clean for the future of your app.但是,如果您有选择权,请使用第二种解决方案,它对您的应用程序的未来更易于维护和清洁。

If you need to provide your custom props and props given by react-router you can use the third solution is:如果您需要提供您的自定义道具和react-router给出的道具,您可以使用第三种解决方案:

<Route exact path="/" render={(props) => <App matches={props} myProps={1} />} />

It depends on if you want/need the route props ( history , location , and match ) passed on to the component.这取决于您是否想要/需要传递给组件的路由道具historylocationmatch )。

<div className="App">
  <Router>
    <Switch>
      <Route
        path="/basics"
        component={BasicsPage} // <-- route props passed
      />
      <Route
        path="/basics"
      >
        <BasicsPage /> // <-- route props not passed
      </Route>
      // other routes
    </Switch>
  </Router>
</div>

However, if you needed to pass additional props on to a component, then the second method allows for that.但是,如果您需要将额外的道具传递给组件,那么第二种方法允许这样做。

<div className="App">
  <Router>
    <Switch>
      <Route
        path="/basics"
      >
        <BasicsPage myCustomProp={someValue} /> // <-- pass custom prop
      </Route>
      // other routes
    </Switch>
  </Router>
</div>

And if you needed the route props and to pass additional props, then use the render prop.如果您需要路线道具传递其他道具,请使用render道具。

<div className="App">
  <Router>
    <Switch>
      <Route
        path="/basics"
        render={routeProps => (
          <BasicsPage
            {...routeProps}          // <-- pass route props
            myCustomProp={someValue} // <-- and pass custom prop
          />
        )}
      />
      // other routes
    </Switch>
  </Router>
</div>

Route render methods : 路由渲染方法

The recommended method of rendering something with a <Route> is to use children elements, as shown below.使用<Route>渲染内容的推荐方法是使用元素,如下所示。 There are, however, a few other methods (see this ) you can use to render something with a <Route> .但是,您可以使用其他一些方法(请参阅this )通过<Route>渲染某些内容。 These are provided mostly for supporting apps that were built with earlier versions of the router before hooks were introduced.这些主要用于支持在引入钩子之前使用早期版本的路由器构建的应用程序。

Example:例子:

<Router>
  <Switch>
    <Route path="/basics">
      <BasicsPage />
    </Route>
    <Route path="/foo">
      <Foo>Some children here</Foo>
    </Route>
  </Switch>
</Router>

Use the above method for declaring all the routes and use hooks (useHistory, useLocation, useParams etc.) to access the router's internal state.使用上述方法声明所有路由并使用钩子(useHistory、useLocation、useParams 等)访问路由器内部 state。 This style will also make it easy to switch to React Router v6 .这种风格也可以很容易地切换到React Router v6 See this as pointed out by @jonrsharpe in the comment .请参阅@jonrsharpe 在评论中指出的一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM