Programmatically navigate using react router

后端 未结 30 2440
無奈伤痛
無奈伤痛 2020-11-21 05:18

With react-router I can use the Link element to create links which are natively handled by react router.

I see internally it calls t

30条回答
  •  面向向阳花
    2020-11-21 05:35

    To do the navigation programmatically, you need to push a new history to the props.history in your component, so something like this can do the work for you:

    //using ES6
    import React from 'react';
    
    class App extends React.Component {
    
      constructor(props) {
        super(props)
        this.handleClick = this.handleClick.bind(this)
      }
    
      handleClick(e) {
        e.preventDefault()
        /* Look at here, you can add it here */
        this.props.history.push('/redirected');
      }
    
      render() {
        return (
          
    ) } } export default App;

提交回复
热议问题