在我的 Express 中,我设置了一个这样的路由器,以便它将Dashboard
在服务器端呈现组件:
app.get('/dashboard', auth.isLoggedIn, function(req, res) {
quotes.findAllByUser(req.user.id).then(function(result){
var Dashboard=require('../component/dashboard.js').Dashboard;
var DashboardHTML=ReactDOMServer.renderToString(
<Dashboard user={req.user} list={result}/>
);
res.render('dashboard.ejs',{dashboard:DashboardHTML});
});
});
在我的组件Dashboard
中,用 React 编写如下:
var React=require('react');
var CreateFragment=require('react-addons-create-fragment');
var Dashboard=React.createClass({
render:function(){
var children=CreateFragment({
childUser:this.props.user,
childList:this.props.list
});
return (
<div classNameN="container">
{children.childUser}
</div>
);
}
});
exports.Dashboard=Dashboard;
然后我启动服务器并在该路由中输入浏览器,并收到此错误:
Unhandled rejection Invariant Violation: Objects are not valid as a React child (found: object with keys {avatar, email, id, name, token, type}). If you meant to render a collection of children, use an array instead or wrap the object using createFragment(object) from the React add-ons. Check the render method of `Dashboard`.
console.log(this.props)
如果我在返回函数之前尝试render
,所有对象都会正常登录到控制台。但我无法渲染它。我以为我已经在片段中包裹props
了路由器传递