admin管理员组文章数量:1391960
I am rendering a react class using node js as so...
var express = require('express');
var router = express.Router();
var React = require('react');
var reactDom = require('react-dom/server');
var App = React.createFactory(require('../ponents/index'));
router.get('/', function(req,res) {
var reactHtml = reactDom.renderToString(App({}));
res.render('../../tutorHub/views/index.jade', {reactOutput: reactHtml});
});
module.exports = router;
The page gets rendered fine, but no function that I add gets called. For example, in my App
class...
class App extends React.Component {
constructor(props) {
super(props);
}
getClass() {
return "a_class";
}
render() {
return (
<div className={this.getClass}></div>
);
}
}
module.exports = App;
The getClass
function is not called. Instead the className bees the code
class = getClass() {
return "a_class";
}
instead of simply a_class
when I check the html. For some reason, rather than the function being called, it is simply saved as a string and placed in to the className.
Why is this happening? I am not able to call any functions I make. Can someone help me out?
I am rendering a react class using node js as so...
var express = require('express');
var router = express.Router();
var React = require('react');
var reactDom = require('react-dom/server');
var App = React.createFactory(require('../ponents/index'));
router.get('/', function(req,res) {
var reactHtml = reactDom.renderToString(App({}));
res.render('../../tutorHub/views/index.jade', {reactOutput: reactHtml});
});
module.exports = router;
The page gets rendered fine, but no function that I add gets called. For example, in my App
class...
class App extends React.Component {
constructor(props) {
super(props);
}
getClass() {
return "a_class";
}
render() {
return (
<div className={this.getClass}></div>
);
}
}
module.exports = App;
The getClass
function is not called. Instead the className bees the code
class = getClass() {
return "a_class";
}
instead of simply a_class
when I check the html. For some reason, rather than the function being called, it is simply saved as a string and placed in to the className.
Why is this happening? I am not able to call any functions I make. Can someone help me out?
Share Improve this question edited Aug 24, 2016 at 0:38 Tamas Hegedus 30k12 gold badges66 silver badges101 bronze badges asked Aug 24, 2016 at 0:31 buydadipbuydadip 9,45722 gold badges93 silver badges160 bronze badges1 Answer
Reset to default 5The function isn't called because you didn't call it. You can see the function's source, because any function that gets coerected to a string will do the same ((""+(foo=>bar)) === "foo=>bar"
). All you have to do is:
<div className={this.getClass()}></div>
本文标签: javascriptreact function not being calledStack Overflow
版权声明:本文标题:javascript - react function not being called - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744716307a2621423.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论