如何在从一个页面导航到另一页面时在响应钩子路由器中传递状态或道具

问题描述 投票:0回答:2

我已经尝试从useRouteshookrouter从一页导航到另一页,但是效果很好。我想通过该路线传递一些物体。我应该如何传递物体?我已经阅读了hookrouter GitHub页面,但仍然没有理解。我尝试过navigate("/home",true,{name:"batman"});,但没有帮助。我该怎么办?

reactjs redux react-router react-navigation react-hooks
2个回答
0
投票

您可以在react-router-dom中使用链接组件。

import { Link } from 'react-router-dom';

<Link to={`home/${obj.name}`}> Click here </Link>

0
投票

如果您使用按钮作为路由事件触发器:

import { Button } from '@material-ui/core';
import { Link } from 'react-router-dom';

<Button
  ...
  component={Link}
  to={{
    pathname: `yourRoutePath`,
    // write your custom state below
    state: {
      otherCustomState: value,
      // sample below
      prevPath: history.location.pathname,
    }
  }}
>
  next page
</Button>

用法

history.location.state.yourCustomState
© www.soinside.com 2019 - 2024. All rights reserved.