前提・実現したいこと
Reactの環境にてfirebase-toolsのonAuthStateChanged関数でuserデータを受け取って保持したいのですが、どういじってもsetStateが使えません。
発生している問題・エラーメッセージ
n.setState is not function
該当のソースコード
JavaScript
1import Firebase, { Firestore } from './Firebase'; 2 3export default class Auth { 4 constructor(props) { 5 this.state = { 6 user: null, 7 }; 8 } 9 10 initialization = () => { 11 Firebase.auth().onAuthStateChanged(function(user) { 12 if (user) { 13 console.log(user); 14 this.setState({ user: user }); // エラー箇所 15 console.log(this.state.user); 16 } else { 17 console.log("user is null"); 18 } 19 }); 20 } 21}
試したこと
this.setStateの行を別の関数にうつして、その関数を呼び出し。
JavaScript
1import Firebase, { Firestore } from './Firebase'; 2 3export default class Auth { 4 constructor(props) { 5 this.state = { 6 user: null, 7 }; 8 } 9 10 initialization = () => { 11 Firebase.auth().onAuthStateChanged(function(user) { 12 if (user) { 13 console.log(user); 14 this.setUserState(user); 15 console.log(this.state.user); 16 } else { 17 console.log("user is null"); 18 } 19 }); 20 } 21 22 setUserState = user => { 23 this.setState({ user: user }); // エラー箇所 24 } 25}
function (user)を別の関数にうつして、その関数を呼び出し。
JavaScript
1import Firebase, { Firestore } from './Firebase'; 2 3export default class Auth { 4 constructor(props) { 5 this.state = { 6 user: null, 7 }; 8 } 9 10 initialization = () => { 11 Firebase.auth().onAuthStateChanged(onAuthStateChanged); 12 } 13 14 onAuthStateChanged = user => { 15 if (user) { 16 console.log(user); 17 this.setUserState(user); 18 console.log(this.state.user); 19 } else { 20 console.log("user is null"); 21 } 22 } 23 24 setUserState = user => { 25 this.setState({ user: user }); // エラー箇所 26 } 27}
それぞれの関数をconstructorでbind(this)。
JavaScript
1export default class Auth { 2 constructor(props) { 3 this.state = { 4 user: null, 5 }; 6 7 this.initialization = this.initialization.bind(this); 8 this.onAuthStateChanged = this.onAuthStateChanged.bind(this); 9 this.setUserState = this.setUserState.bind(this); 10 } 11 12 initialization = () => { 13 Firebase.auth().onAuthStateChanged(onAuthStateChanged); 14 } 15 16 onAuthStateChanged = user => { 17 if (user) { 18 console.log(user); 19 this.setUserState(user); 20 console.log(this.state.user); 21 } else { 22 console.log("user is null"); 23 } 24 } 25 26 setUserState = user => { 27 this.setState({ user: user }); // エラー箇所 28 } 29}
useStateに置き換え。
エラー→https://reactjs.org/docs/error-decoder.html/?invariant=321
JavaScript
1import { useState } from "react"; 2import Firebase, { Firestore } from './Firebase'; 3 4export default class Auth { 5 constructor(props) { 6 this.initialization = this.initialization.bind(this); 7 } 8 9 initialization = () => { 10 Firebase.auth().onAuthStateChanged(user => { 11 const [user_, setUser] = useState(null); // エラー箇所 12 if (user) { 13 console.log(user); 14 setUser(user); 15 console.log(user_); 16 } else { 17 console.log("user is null"); 18 } 19 }); 20 } 21}
Promiseを使用
JavaScript
1import Firebase, { Firestore } from './Firebase'; 2 3export default class Auth { 4 constructor(props) { 5 this.state = { 6 user: null, 7 }; 8 9 this.initialization = this.initialization.bind(this); 10 } 11 12 initialization = () => { 13 return new Promise((resolve, reject) => { 14 Firebase.auth().onAuthStateChanged(user => { 15 if (user) { 16 resolve(user); 17 } else { 18 reject(null); 19 } 20 }); 21 }).then(user => { 22 console.log(user); 23 this.setState({ user: user }); // エラー箇所 24 console.log(this.state.user); 25 }).catch(() => console.log("user is null")); 26 } 27}
補足情報(FW/ツールのバージョンなど)
"@testing-library/jest-dom": "^5.11.6", "@testing-library/react": "^11.2.2", "@testing-library/user-event": "^12.6.0", "date-fns": "^2.16.1", "esri-leaflet-geocoder": "^2.3.4", "firebase": "^8.2.1", "firebase-tools": "^9.2.0", "formik": "^2.2.6", "leaflet": "^1.7.1", "react": "^17.0.1", "react-datepicker": "^3.3.0", "react-dom": "^17.0.1", "react-leaflet": "^3.0.5", "react-router-dom": "^5.2.0", "react-scripts": "4.0.0", "styled-components": "^5.2.1", "typescript": "^4.1.3", "web-vitals": "^0.2.4"
回答1件
あなたの回答
tips
プレビュー