3
私はExpressとReactを使ってゲームを構築しています。ユーザのスコアを増やすなど、私のコントローラ上でアクションを実行するには、index.jsx
ファイルのuserIdにアクセスする必要があります。pugからJSXへのパラメータを渡す
私のルートはuser_id
のparamを通過する間index.pug
ファイルをレンダリング:
//server.js
app.get('/', function (req, res) {
const userId = req.query.userId
res.render('index', {userId: userId})
})
それから私はこのように、私のパグファイルにuserId
にアクセスすることができます。
// index.pug
body
div#errors
#root
p #{userId} // prints the User Id
今、私はこれにアクセスする必要がありますuserId
param in index.jsx
ファイルには、Reactコンポーネントが含まれています。
class Cell extends React.Component {
render() {
return (
<button onClick={() => this.props.onClick()}>
{userId}
</button>
)
}
}
しかし、これは期待通りに機能しません。何か案は?
として
react
コンポーネントでuserId
にアクセスすることができ、あなたの.pug
ファイルにしましょう'window'オブジェクトは' pug'データを 'react'コンポーネントに渡します –ありがとう!それは確かに方法です。ここに私が実装した方法があります: '' 'index.pug スクリプト。 window.fbId =!{JSON.stringify(userId)}; '' ' その後index.jsx中: ' '' ReactDOM.render( <ゲームのuserId = {userIdを} />、 のdocument.getElementById( 'ルート') )は '' ' –
ああ、あなたは'に合格レンダリング機能を提供する。それはまた大丈夫です。 –