React BrowserRouter not working with nested path

时光毁灭记忆、已成空白 提交于 2021-02-08 06:41:07

问题


I am trying to map a path to component using react BrowserRouter, all of the parent route works -

<Route path="/storeadmin" render={(props) => <MainTableComponent isUpdateScreen={false} tableData={this.state.tableData}/>} />
<Route path='/Attributs' render={({ history }) => <AttributsTable history={history} allSkus={this.state.allSkus} allAttrNames={this.state.allAttrName} updateViewTypeForNew={this.setViewType} />} />
<Route path='/Updates' render={({ history }) => <MainTableComponent isUpdateScreen={true} />} /> 

But when I try to define a nested route /storeadmin/webkeytopayer-

<Route path='/storeadmin/webkeytopayer' render={({ history }) => <WebKeyTableComponent disableSearchBar={this.triggerBarDisplay} />} />

It redirect to /storeadmin component MainTableComponent instead of WebKeyTableComponent. I tried using 'exact' -

<Route exact path='/storeadmin/webkeytopayer' render={({ history }) => <WebKeyTableComponent disableSearchBar={this.triggerBarDisplay} />} />

But with the same result. Entire routing -

        <BrowserRouter>
            <Switch>
                <Route path="/storeadmin" render={(props) => <MainTableComponent isUpdateScreen={false} tableData={this.state.tableData}
                    updateViewTypeForNew={this.setViewType}
                    searchTerm={this.state.searchValue} refreshTable={this.refreshTable} />} />

                <Route exact={true} path="/" render={(props) => <MainTableComponent isUpdateScreen={false} tableData={this.state.tableData}
                    updateViewTypeForNew={this.setViewType}
                    searchTerm={this.state.searchValue} refreshTable={this.refreshTable} />} />

                <Route path='/Attributs' render={({ history }) => <AttributsTable history={history} allSkus={this.state.allSkus} allAttrNames={this.state.allAttrName} updateViewTypeForNew={this.setViewType} />} />
                <Route path='/Updates' render={({ history }) => <MainTableComponent isUpdateScreen={true} />} />
                <Route exact path='/storeadmin/webkeytopayer' render={({ history }) => <WebKeyTableComponent disableSearchBar={this.triggerBarDisplay} />} />
                <Route  path='/webkeytopayer' render={({ history }) => <WebKeyTableComponent disableSearchBar={this.triggerBarDisplay} />} />

            </Switch>
        </BrowserRouter>

import - import { BrowserRouter, Route, Switch } from 'react-router-dom'; Version -

"react-router-dom": {
      "version": "4.3.1",
      "resolved": "https://registry.npmjs.org/react-router-dom/-/react-router-dom-4.3.1.tgz",
      "integrity": "sha512-c/MlywfxDdCp7EnB7YfPMOfMD3tOtIjrQlj/CKfNMBxdmpJP8xcz5P/UAFn3JbnQCNUxsHyVVqllF9LhgVyFCA==",
      "requires": {
        "history": "^4.7.2",
        "invariant": "^2.2.4",
        "loose-envify": "^1.3.1",
        "prop-types": "^15.6.1",
        "react-router": "^4.3.1",
        "warning": "^4.0.1"
      }
    }

回答1:


So react Router does not support subdomains - React-router subdomain routing But I found the workaround -

<Route
  path="/storeadmin"
  render={(props) => {
    const onWebkey = window.location.pathname.indexOf('webkeytopayer') !== -1
    if (onWebkey)
      return (
        <WebKeyTableComponent disableSearchBar={this.triggerBarDisplay} />
      )
    return (
      <MainTableComponent
        isUpdateScreen={false}
        tableData={this.state.tableData}
        updateViewTypeForNew={this.setViewType}
        searchTerm={this.state.searchValue}
        refreshTable={this.refreshTable}
      />
    )
  }}
/>



回答2:


You can do nested routing using react router:

Let's say you have these routes:

<Route
    // exact // This should not be exact as it have nested routes
    path="/storeadmin"
    render={(props) => (
        <MainTableComponent
        isUpdateScreen={false}
        tableData={this.state.tableData}
        />
    )}
    />
<Route
    path="/Attributs"
    render={({ history }) => (
        <AttributsTable
        history={history}
        allSkus={this.state.allSkus}
        allAttrNames={this.state.allAttrName}
        updateViewTypeForNew={this.setViewType}
        />
    )}
    />
<Route
    path="/Updates"
    render={({ history }) => <MainTableComponent isUpdateScreen={true} />}
    />

To create nested routing for /storeadmin route, you can define nested routes inside MainTableComponent:

// inside, render (class) or return of function, MainTableComponent:

import {
  Switch,
  Route,
  Link,
  withRouter,
} from 'react-router-dom'

class MainTableComponent extends Component {
  render() {
    const { path, url } = this.props.match
    return (
      <>
        <div>I am MainTableComponent</div>
        <Link to={`${url}/webkeytopayer`}>
          Go to Webkeytopayer
        </Link>
        <Switch>
            <Route
                exact // This can be exact as it's the last level route
                path={`${path}/webkeytopayer`}
                render={({ history }) => (
                    <WebKeyTableComponent disableSearchBar={this.triggerBarDisplay} />
                )}
            />
        </Switch>
      </>
    )
  }
}

export default withRouter(MainTableComponent)


来源:https://stackoverflow.com/questions/62022904/react-browserrouter-not-working-with-nested-path

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!