简体   繁体   中英

React Router DOM: Change match in path

In my app I have a router set up like this

  <BrowserRouter basename={props.base_path}>
    <Switch>
      <Route
        path="/views/:view_id/card"
        render={(routerProps) => (
          <>
            <Toolbar {...routerProps} />
          </>
        )}
      ></Route>
    </Switch>
  </BrowserRouter>

Inside Toolbar I would like to be able to change the value of:view_id. I know that using useHistory I can replace parts of the path but I'm not aware of a way I can specifically target the:view_id part of the path.

What would be the most correct way to change this matched variable with react-router-dom?

One option I have found is to just pass the path down to the Toolbar and to do something like

history.push(props.path.replace(':view_id', opt.value))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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