我正在使用Express和React构建一款游戏。我需要在我的index.jsx
文件中访问userId来执行控制器上的操作,比如增加用户分数。
我的路由渲染一个index.pug
文件,并传递一个user_id
参数:
//server.js
app.get('/', function (req, res) {
const userId = req.query.userId
res.render('index', {userId: userId})
})
然后我可以在pug文件中访问
userId
,像这样:// index.pug
body
div#errors
#root
p #{userId} // prints the User Id
现在我需要在包含React组件的
index.jsx
文件中访问这个userId
参数。class Cell extends React.Component {
render() {
return (
<button onClick={() => this.props.onClick()}>
{userId}
</button>
)
}
}
但是那样做并不起作用,正如我所预料的那样。有什么想法吗?
window
对象以相同的方式将pug
数据传递给react
组件。 - Jyothi Babu Arajascript. window.fbId = !{JSON.stringify(userId)};
然后在 index.jsx 中:render
函数了。那也没问题。 - Jyothi Babu Araja