【发布时间】: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