react-router这是未定义的

时间:2015-05-06 17:52:12

标签: javascript reactjs react-router

未捕获的TypeError:无法读取未定义的属性'dispatch'

这发生在handleLocationChange:

中的ReactRouter.js中
      handleLocationChange: function handleLocationChange(change) {
        this.dispatch(change.path, change.type);
      },

这是我打电话

的下游

this.context.transitionTo( '某物');

其中'something'是我定义的路线之一。为什么“这个”不确定?

这是我调用代码的组件:

var React = require("react");
var Router = require("react-router");
var { Link } = Router;
var Button = require('core-ui').Button;

var AppointmentsHeader = React.createClass({
    mixins: [React.addons.PureRenderMixin],
    contextTypes: {
        router: React.PropTypes.func
    },

    render: function () {
        console.log("AppointmentsHeader:render");
        var router = this.context.router;
        // default to hidden
        var displayClassInline = "appointments-hide"; // hide/show this element if the current page is Landing Page

        if (this.props.currentState.get('currentState') === "landingPage")
        {
            displayClassInline = "appointments-block-show";
        }
        return (
            <div className={"appointments-header cv-grid " + displayClassInline}>
                <div className="appointments-title">Appointments</div>
                <Button label="Create Appointment Event" style="primary" onClick={this._onClick}/>
            </div>
        );
    },
    _onClick: function() {
        console.log("AppointmentsHeader 'Create Appointment Event' button clicked");
        var newStatus = this.props.currentState.set('currentState', "CreateAppointment");
        this.props.handleChange(newStatus);
        this.context.transitionTo('createAppointmentsEvent');
    }
});

module.exports = AppointmentsHeader;

1 个答案:

答案 0 :(得分:0)

这一行:

this.context.transitionTo('createAppointmentsEvent')

应该是:

this.context.router.transitionTo('createAppointmentsEvent');

this.transitionTo('createAppointementsEvent')

您在示例中以两种不同的方式访问单例路由器:

  • 导航mixin
  • 通过contextTypes哈希
  • 注入的路由器

API现在非常令人困惑,因为Ryan走了一条路(完全摆脱mixins)然后决定&#34; undepricate&#34;混合物。