Reactjs 如何通过history.push()传递变量id

Reactjs 如何通过history.push()传递变量id,reactjs,typescript,react-router,react-hooks,tsx,Reactjs,Typescript,React Router,React Hooks,Tsx,我使用react路由器库来导航,使用history.push()来导航onClick。当用户单击一行时,他们将被导航到“其他页面”。在“otherPage”中,我想使用变量“id”。如何传递这个变量 function handleRowClick(id: number): void { // navigates user to another page history.push('/otherPage'); } history.push(路由、状态); 可选的状态参数是带有自

我使用react路由器库来导航,使用history.push()来导航onClick。当用户单击一行时,他们将被导航到“其他页面”。在“otherPage”中,我想使用变量“id”。如何传递这个变量

function handleRowClick(id: number): void {
    // navigates user to another page
    history.push('/otherPage');
}
history.push(路由、状态);
可选的
状态
参数是带有自定义路由参数的对象

state
对象在组件内部(如果没有,可以用

this.props.location.state
类组件 高阶分量 功能部件 高阶分量 反作用钩
从'react router dom'导入{useLocation};
/*其他依赖项*/
常量YourFunctionalComponent=({location})=>{
const location=useLocation();
console.log(location.state);
返回(
/*这里有一些jsx*/
);
};
历史。推送(路线、状态);
可选的
状态
参数是带有自定义路由参数的对象

state
对象在组件内部(如果没有,可以用

this.props.location.state
类组件 高阶分量 功能部件 高阶分量 反作用钩
从'react router dom'导入{useLocation};
/*其他依赖项*/
常量YourFunctionalComponent=({location})=>{
const location=useLocation();
console.log(location.state);
返回(
/*这里有一些jsx*/
);
};

好的,谢谢,但是当使用React hook时,我如何使用“otherPage”中的变量?@Io7我添加了关于
with router
hoc的信息,我希望它能有所帮助:)好的,谢谢,但是在使用React钩子时,如何使用“otherPage”中的变量?@Io7我添加了关于
withRouter
hoc的信息,我希望这会有所帮助:)请不要在标题中添加“标签”,这就是标签的用途。请不要在标题中添加“标签”,这就是标签的用途。
import { withRouter } from 'react-router';
/* other dependencies */

class YourClassComponent {
  render() {
    const { location } = this.props;
    console.log(location.state);

    return (
      /* some jsx here */ 
    );
  }
}

export default withRouter(YourClassComponent);
import { withRouter } from 'react-router';
/* other dependencies */

const YourFunctionalComponent = ({ location }) => {
  console.log(location.state);

  return (
    /* some jsx here */ 
  );
};

export withRouter(YourFunctionalComponent);