Programmatically navigate using react router

后端 未结 30 2286
無奈伤痛
無奈伤痛 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:43

    Warning: this answer covers only ReactRouter versions before 1.0

    I will update this answer with 1.0.0-rc1 use cases after!

    You can do this without mixins too.

    let Authentication = React.createClass({
      contextTypes: {
        router: React.PropTypes.func
      },
      handleClick(e) {
        e.preventDefault();
        this.context.router.transitionTo('/');
      },
      render(){
        return (<div onClick={this.handleClick}>Click me!</div>);
      }
    });
    

    The gotcha with contexts is that it is not accessible unless you define the contextTypes on the class.

    As for what is context, it is an object, like props, that are passed down from parent to child, but it is passed down implicitly, without having to redeclare props each time. See https://www.tildedave.com/2014/11/15/introduction-to-contexts-in-react-js.html

    0 讨论(0)
  • 2020-11-21 05:43

    May not be the best approach but... Using react-router v4, the following Typescript could give an idea for some.

    In the rendered component below, e.g. LoginPage, router object is accessible and just call router.transitionTo('/homepage') to navigate.

    Navigation code was taken from.

    "react-router": "^4.0.0-2", "react": "^15.3.1",

    import Router from 'react-router/BrowserRouter';
    import { History } from 'react-history/BrowserHistory';
    import createHistory from 'history/createBrowserHistory';
    const history = createHistory();
    
    interface MatchWithPropsInterface {
      component: typeof React.Component,
      router: Router,
      history: History,
      exactly?: any,
      pattern: string
    }
    
    class MatchWithProps extends React.Component<MatchWithPropsInterface,any> {
      render() {
        return(
          <Match {...this.props} render={(matchProps) => (
                 React.createElement(this.props.component, this.props)
    
            )}
           />
        )
      }
    }
    
    ReactDOM.render(
        <Router>
          {({ router }) => (
            <div>
              <MatchWithProps exactly pattern="/" component={LoginPage} router={router} history={history} />
              <MatchWithProps pattern="/login" component={LoginPage} router={router} history={history} />
              <MatchWithProps pattern="/homepage" component={HomePage} router={router} history={history} />
              <Miss component={NotFoundView} />
            </div>
          )}
        </Router>,
    
       document.getElementById('app')
    );

    0 讨论(0)
  • 2020-11-21 05:45

    The right answer was for me at the time of writing

    this.context.router.history.push('/');
    

    But you need to add PropTypes to your component

    Header.contextTypes = {
      router: PropTypes.object.isRequired
    }
    export default Header;
    

    Don't forget to import PropTypes

    import PropTypes from 'prop-types';
    
    0 讨论(0)
  • 2020-11-21 05:49

    Here's how you do this with react-router v2.0.0 with ES6. react-router has moved away from mixins.

    import React from 'react';
    
    export default class MyComponent extends React.Component {
      navigateToPage = () => {
        this.context.router.push('/my-route')
      };
    
      render() {
        return (
          <button onClick={this.navigateToPage}>Go!</button>
        );
      }
    }
    
    MyComponent.contextTypes = {
      router: React.PropTypes.object.isRequired
    }
    
    0 讨论(0)
  • 2020-11-21 05:49

    In react router v4. I follow this two way to route programmatically.

    1. this.props.history.push("/something/something")
    2. this.props.history.replace("/something/something")
    

    Number two

    Replaces the current entry on the history stack

    To get history in props you may have to wrap your component with

    withRouter

    0 讨论(0)
  • 2020-11-21 05:49

    You can also use the useHistory hook in a stateless component. Example from the docs.

    import { useHistory } from "react-router"
    
    function HomeButton() {
      const history = useHistory()
    
      return (
        <button type="button" onClick={() => history.push("/home")}>
          Go home
        </button>
      )
    }
    

    Note: Hooks were added in react-router@5.1.0 and require react@>=16.8

    0 讨论(0)
提交回复
热议问题