【问题标题】:react-router this is undefined反应路由器这是未定义的
【发布时间】:2015-05-06 17:52:11
【问题描述】:

Uncaught TypeError: Cannot read property 'dispatch' of undefined

这发生在 ReactRouter.js 的 handleLocationChange 中:

      handleLocationChange: function handleLocationChange(change) {
        this.dispatch(change.path, change.type);
      },

这是我调用的下游

this.context.transitionTo('something');

“某事”是我定义的路线之一。为什么 'this' 是未定义的?

这是我调用代码的组件:

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;

【问题讨论】:

  • 您能否提供更多有关该代码 sn-p 来自何处的上下文?基本上向我们展示更多代码(最好是整个组件)。
  • 该代码在 ReactRouter 中,这是我的组件调用方法: _onClick() { 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;
  • 我用你的代码编辑了你的问题,虽然这对我来说似乎不正确......不应该是 _onClick: function() { ... 吗?否则,您只是在那里运行_onClick,而实际上从未对它做任何事情。如果可能,请使用您的完整代码编辑问题本身。
  • 我认为至少 react-router 示例已经过时,所以我在它们上打开了一个问题:react router issue
  • 请编辑您的问题,不要将代码块放入 cmets。

标签: javascript reactjs react-router


【解决方案1】:

这一行:

this.context.transitionTo('createAppointmentsEvent')

应该是:

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

this.transitionTo('createAppointementsEvent')

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

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

API 现在非常混乱,因为 Ryan 走了一条路(完全摆脱了 mixins),然后决定“贬低”这些 mixins。

【讨论】:

  • 抱歉,“路由器”在 contect 上不可用,但 transitionTo 在那里。升级指南(用于回答其他 SO 问题)特别指出 Upgrade Guide:“如果没有 mixins,我们需要一种方法来让您访问这些方法。我们认为最简单的解决方案是停止隐藏路由器实例,只需将整个事情都在上下文中。”
  • 我没有意识到我什至还有 Navigation mixin,根据升级指南,这是一个错误,我尝试删除它,但我的上下文中仍然没有“路由器”。之后,尝试直接在上下文中访问 transitionTo (就像升级指南所说的那样)会产生这个错误:“Uncaught TypeError: this.context.transitionTo is not a function”。
  • Ryan 建议它应该是 this.transitionTo 并且升级指南是错误的,就像示例一样。
猜你喜欢
  • 1970-01-01
  • 2017-11-02
  • 1970-01-01
  • 1970-01-01
  • 2016-04-08
  • 1970-01-01
  • 2017-12-21
  • 2019-12-25
  • 1970-01-01
相关资源
最近更新 更多