Programmatically navigate using react router

后端 未结 30 2371
無奈伤痛
無奈伤痛 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 (
    Click me!
    ); } });

    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

提交回复
热议问题