我正在使用React-Router和Alt在this tutorial之后在ES6中构建一个React Web应用程序。我的所有组件都正确呈现,但是在刷新浏览器页面时,那些具有构造函数的组件会中断。这是错误。
TypeError: undefined is not a function
at new Track (/Users/juancarlosfarah/Git/maestro/src/components/Track.js:14:17)
at [object Object].ReactCompositeComponentMixin.mountComponent (/Users/juancarlosfarah/Git/maestro/node_modules/react/lib/ReactCompositeComponent.js:135:16)
at [object Object].wrapper [as mountComponent] (/Users/juancarlosfarah/Git/maestro/node_modules/react/lib/ReactPerf.js:70:21)
at Object.ReactReconciler.mountComponent (/Users/juancarlosfarah/Git/maestro/node_modules/react/lib/ReactReconciler.js:38:35)
at [object Object].ReactCompositeComponentMixin.mountComponent (/Users/juancarlosfarah/Git/maestro/node_modules/react/lib/ReactCompositeComponent.js:247:34)
at [object Object].wrapper [as mountComponent] (/Users/juancarlosfarah/Git/maestro/node_modules/react/lib/ReactPerf.js:70:21)
at Object.ReactReconciler.mountComponent (/Users/juancarlosfarah/Git/maestro/node_modules/react/lib/ReactReconciler.js:38:35)
at [object Object].ReactCompositeComponentMixin.mountComponent (/Users/juancarlosfarah/Git/maestro/node_modules/react/lib/ReactCompositeComponent.js:247:34)
at [object Object].wrapper [as mountComponent] (/Users/juancarlosfarah/Git/maestro/node_modules/react/lib/ReactPerf.js:70:21)
at Object.ReactReconciler.mountComponent (/Users/juancarlosfarah/Git/maestro/node_modules/react/lib/ReactReconciler.js:38:35)
at ReactDOMComponent.ReactMultiChild.Mixin.mountChildren (/Users/juancarlosfarah/Git/maestro/node_modules/react/lib/ReactMultiChild.js:192:44)
at ReactDOMComponent.Mixin._createContentMarkup (/Users/juancarlosfarah/Git/maestro/node_modules/react/lib/ReactDOMComponent.js:289:32)
at ReactDOMComponent.Mixin.mountComponent (/Users/juancarlosfarah/Git/maestro/node_modules/react/lib/ReactDOMComponent.js:199:12)
at Object.ReactReconciler.mountComponent (/Users/juancarlosfarah/Git/maestro/node_modules/react/lib/ReactReconciler.js:38:35)
at [object Object].ReactCompositeComponentMixin.mountComponent (/Users/juancarlosfarah/Git/maestro/node_modules/react/lib/ReactCompositeComponent.js:247:34)
at [object Object].wrapper [as mountComponent] (/Users/juancarlosfarah/Git/maestro/node_modules/react/lib/ReactPerf.js:70:21)
在Track.js的第14行,定义了构造函数:
class Track extends React.Component {
constructor(props) {
super(props);
console.debug("Constructing Track...");
this.state = TrackStore.getState();
this.handleTextChange = this.handleTextChange.bind(this);
this.handleSaveClick = this.handleSaveClick.bind(this);
this.handleUndoClick = this.handleUndoClick.bind(this);
this.renderButtons = this.renderButtons.bind(this);
this.onChange = this.onChange.bind(this);
}
...
}
在app.js中,我按如下方式渲染路线:
Router.run(routes, Router.HistoryLocation, function(Handler) {
React.render(<Handler />, document.getElementById('app'));
});
它们在routes.js中定义:
export default (
<Route handler={App}>
<Route path='/' handler={Home} />
<Route path="/tracks/" handler={Tracks} />
<Route path="/track/:id" handler={Track} />
</Route>
);
在server.js中,使用Swig渲染页面。
app.use(function(req, res) {
Router.run(routes, req.path, function(Handler) {
let html = React.renderToString(React.createElement(Handler));
let page = swig.renderFile('views/index.html', { html: html });
res.send(page);
});
});
即使我删除constructor
中的所有代码并只留下console.log
语句,我刷新浏览器时也会收到错误。如果没有构造函数,则刷新时页面呈现正常。
答案 0 :(得分:2)
经过很长一段时间,我想出了我遇到的问题。在Track.js
中我添加了一行来帮助我调试错误,它调用console.debug()
(我现在已经明确地将其添加到问题中的代码中,但我最初没有显示它)。当客户端呈现Track.js
时,这不是问题,因为Chrome中的控制台可以运行console.debug()
。但是,当直接访问URL时,页面将由服务器呈现,该服务器不支持console.debug()
。因此,应用程序将破坏并显示出神秘的错误。删除该行或将其更改为console.log()
可解决此问题。