Reputation: 9407
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('../components/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 becomes 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?
Upvotes: 1
Views: 7271
Reputation: 29906
The 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>
Upvotes: 5