今 react natice で開発をしているのですがJSの変数のスコープに苦しめられております。
foreach内で使用した変数をその外で使いたいのですがスコープによって使えません。
この様な場合どの様にするのがベストなのでしょうか。
countNumberという変数を一番下のif文でも使いたいのですがこのままだと一番下のif文のcountNumberは常に0になってしまいます。
var countNumber = 0; firebaseRef .on("value", function(snapshot) { snapshot.forEach(function (childSnapshot) { // var countNumber = 0; const childData = childSnapshot.val(); const savedUserId = childData.userId; if (loginUserId === savedUserId ){ countNumber += 1 } }) }); if (countNumber === 0){ this.setState({ isModalVisible: !this.state.isModalVisible }); console.log(this.state.isModalVisible); }
「React.js」もタグに入れておいてください。
回答2件
あなたの回答
tips
プレビュー