简体   繁体   English

如何在反应中为单个页面中的多个组件进行嵌套路由

[英]How to do nested routing for multiple components in single page in react

I am displaying three components in one single App.js.我在一个 App.js 中显示三个组件。 I need to route these components: I have main container CSV.js in which two components TABLE.js and FILEUPLOAD.js is present.我需要路由这些组件:我有主容器 CSV.js,其中存在两个组件 TABLE.js 和 FILEUPLOAD.js。 The main route would be /csv-app.主要路线是/csv-app。 When a row in table is clicked the route should be /csv-app/table/:id单击表中的一行时,路由应为 /csv-app/table/:id

If you are using React Router the problem is quite easy solvable.如果你使用React Router ,这个问题很容易解决。 See the nested routing example.请参阅嵌套路由示例。

you should get something like this using React Router Dom你应该使用 React Router Dom 得到类似的东西

import { Switch, Route, Link, Redirect, BrowserRouter as Router } from "react-router-dom";

const Table = props =>(
    <Switch>
        <Route exact path="/csv-app/table/:id">
            "table row ....."
        </Route>

        <Route axact path="/csv-app/table">
            <table>
                <Link to={`/csv-app/table/${2}`}></Link>
            </table>
        </Route>
    </Switch>
)

const FileUpload = props => (<> file upload component </>)

const Csv = props => (
<Switch>
    <Route exact path='/csv-app/table'>
        <Table />
    </Route>
    <Route exact path='/csv-app/fileupload'>
        <FileUpload />
    </Route>

    <Redirect to='/csv-app' />
</Switch>
)


const App = props => (
<Router basename="/">
    <Csv />
</Router>
)

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

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