我建立了一个新的react项目,由于某种原因,该componentDidMount方法没有被调用。
componentDidMount
我已经通过调用console.login 验证了此行为,componentDidMount但是在控制台中看不到它的输出。
console.log
此外,this.setState()无法正常工作。
this.setState()
关于为什么componentDidMount不调用,我感到很困惑。我尝试同时使用React“ v0.14.0”和“ v0.14.3”。
为什么不调用“ componentDidMount”?
码:
var React = require('react'); var RecipePage = React.createClass({ componentDidMount: function() { console.log('mounted!'); }, render: function() { return ( <div>Random Page</div> ); } }); module.exports = RecipePage;
所以…瞧瞧......我终于开始工作了(在后端专家的帮助下)。之所以未触发“ componentDidMount”方法,是因为 我的服务器正在捆绑并在服务器端提供所有的react + html服务 。(只有调用“ render”和“ getInitialState”方法才能创建“ html”模板,该模板通过客户端的浏览器传递…但是它停在那里,因为它认为已经完成了)
解决方法: 找到一种通过服务器交付生成的“已编译” html的方法,此外,还 可以在客户端再次访问和“触发” react自己的事件 。当编译我的“ view”文件(index.js或index.html)时,我包括了一个“ Application.start()”脚本,该脚本将我的bundle.js代码再次注入模板中。然后在我的gulpfile中,导出“ Application”变量,以便“ view”文件可以访问它。
Gahh …为此拉了我的头发。是时候阅读 服务器端和客户端渲染了