【问题标题】:How to navigate to other page using react router如何使用反应路由器导航到其他页面
【发布时间】:2023-03-16 17:25:02
【问题描述】:

我有一个 onClick 功能可以导航到其他页面。我尝试了 this.props.history.push("/SecondPage/ID/") 和一些示例,但没有成功。

我有这样的组件:

export class MainPage extends Component {  
  constructor(props) {
    super(props);
  }
  render(){
    return (
        <div id="main" onClick={this.NavigatetoOtherPage.bind(this)}>
    )
  }  
  NavigatetoOtherPage(){
     let ID = this.props.ID; // I need to pass the ID as a parameter.
      //Here I need to navigate to other page using. I can use window.location.href but I need to use react router.
  }
}
export default connect(state => {
return {
    ID: state.Reducer.ID,       
};
})(MainPage)

我的 app.js 文件是这样的

export default class App extends Component {

render() {
    return (
        <Provider store={store}>               
            <Route exact path='/' component={MainPage}/>               
            <Route path='/SecondPage/:ID/' component = {SecondPage} />
        </Provider>
    );
}
}

我的 index.js 页面是这样的

export function renderPage() {  
ReactDOM.render(
    <Router>
        <App />
    </Router>
    , document.getElementById('root'));

}
renderPage();

如何在没有 window.location.href 的情况下导航到第二页

【问题讨论】:

  • 你有什么理由不使用 吗?
  • 路由“/path”应该渲染什么?你的地址正确吗?
  • @İlker 我可以使用链接。但是我们如何在 OnClick 或 OnDoubleClick 中使用 Link?
  • @alisasani 路径是“/SecondPage/ID/”。
  • 请提供更多细节。我根据您的代码在此处创建了一个沙盒链接 (codesandbox.io/s/react-router-forked-bf13w?file=/index.js),您可以看到它运行良好。

标签: reactjs react-router react-router-dom


【解决方案1】:

你可以使用useHistory钩子或Link组件,因为你正在使用react-router-dom

import React from "react";
import { useHistory, Link } from "react-router-dom";

 // Then in your component
 const MainPage = (props) => {
  /**
   * hooks
   */
  const history = useHistory();

  /**
   * function
   */
  const handleNavigation = () => {
   let ID = props.ID; // I need to pass the ID as a parameter.
  
    history.push(`/dashboard/${ID}`)
  }

  return (
    <button id="main" onClick={() => history.push("/")}> Go to / </button>
    <button id="main" onClick={() => handleNavigation()}> Go to dynamic page 
    </button>
    <Link to={`/dashboard/${props.ID}`} className="some-styling">
      Using Link
    </Link>
  );
};

// I have merged both implementations



export default MainPage;

// 已编辑:根据评论,问题是“历史没有出现在道具中。”

// Then you could use `withRouter` HOC, and then there will be 
// the `history` object in the wrapped component's props.

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


class MainPage extends React.Component {

  render(){
    console.log(this.props.history) // history object
    return(<div />)
  }
}

export default withRouter(MainPage)`

【讨论】:

  • 我只是给了你示例。我现在不能使用钩子,因为我的实际文件很大。
  • @AshokYaganti 我已经根据您在 cmets 中提到的无法“从道具访问历史记录”更新了上述内容。
  • withRouter 为我工作。非常感谢。
【解决方案2】:

写了一个小沙盒。我想这就是你想要实现的目标。

https://codesandbox.io/s/practical-tereshkova-ilbig?file=/src/App.js

【讨论】:

  • 我尝试了同样的事情。但我不知道历史不是来自道具。这是由于 redux 连接造成的吗?
  • 我已经添加了 redux 连接。仍然可以正常工作。出于演示目的,我添加了调度以增加 ID。您可以参考相同的链接。
猜你喜欢
  • 2018-07-28
  • 2017-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-17
  • 2021-03-29
  • 1970-01-01
  • 2016-05-29
相关资源
最近更新 更多