
React์์ ์ํ ๊ด๋ฆฌ๋ฅผ ํ ๋ Redux๋ฅผ ์ฐ๋ ๊ฒฝ์ฐ๊ฐ ๋ง๊ณ ,
๋ฐฑ์๋ ์์ด ๋น ๋ฅด๊ฒ ์๋ฒ ๊ธฐ๋ฅ์ ๋ถ์ผ ๋ Firebase๊ฐ ์ ์ฉํ๋ค.
๊ทธ๋ฐ๋ฐ ์ด ๋์ ํจ๊ป ์ธ ๋ ๊ตฌ์กฐ๋ฅผ ์ด๋ป๊ฒ ์ก์์ผ ํ ์ง ๊ณ ๋ฏผ์ด ์๊ธฐ๊ธฐ ๋ง๋ จ์ด๋ค.
์ค๋์ Firebase์ Redux Store๊ฐ ๊ฐ๊ฐ ์ด๋ค ์ญํ ์ ํ๊ณ ,
์ด๋ป๊ฒ ํจ๊ป ์ฌ์ฉํ ์ ์๋์ง ์ ๋ฆฌํด๋ณธ๋ค.
Firebase๋ Google์์ ์ ๊ณตํ๋ BaaS(Backend as a Service)๋ก,
๋ฐฑ์๋ ํ๊ฒฝ ์์ด๋ ์ฝ๊ฒ ์๋ฒ ๊ธฐ๋ฅ์ ๊ตฌํํ ์ ์๋ค.
์ฃผ์ ๊ธฐ๋ฅ์ ๋ค์๊ณผ ๊ฐ๋ค:
Firebase๋ฅผ ์ฐ๋ฉด ํ๋ก ํธ์๋์์ ๋ฐ๋ก ๋ก๊ทธ์ธ, ๋ฐ์ดํฐ ์ ์ฅ, ํ์ผ ์
๋ก๋ ๋ฑ์ ์ฒ๋ฆฌํ ์ ์๋ค.
Redux๋ React์ ์ํ ๊ด๋ฆฌ๋ฅผ ๋ ์ฒด๊ณ์ ์ผ๋ก ํ ์ ์๊ฒ ๋์์ฃผ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ค.
Redux Store๋ ์ ํ๋ฆฌ์ผ์ด์ ์ ์ฒด์ ์ํ๋ฅผ ์ค์ ์ง์คํํด ๊ด๋ฆฌํ๋ ๊ณต๊ฐ์ด๋ค.
import { createStore } from 'redux';
import rootReducer from './reducers';
const store = createStore(rootReducer);
Redux Store๋ ํฌ๊ฒ ๋ค์๊ณผ ๊ฐ์ ๊ตฌ์ฑ์์๋ก ๋๋๋ค:
๋ ๊ฐ๋ฅผ ๊ฐ์ด ์ฐ๋ฉด ์ด๋ ๊ฒ ๋๋ ์ ์๋ค:
| ์ญํ | ์ฌ์ฉ ๋๊ตฌ |
|---|---|
| ์ฌ์ฉ์ ์ธ์ฆ | Firebase Auth |
| ๋ฐ์ดํฐ ์ ์ฅ/์ฝ๊ธฐ | Firebase Firestore |
| ๋ก์ปฌ ์ํ ๊ด๋ฆฌ | Redux Store |
| ์ ์ญ ์ํ ๊ด๋ฆฌ | Redux Store |
์ฆ, Firebase๋ ๋ฐฑ์๋ ์ญํ , Redux๋ ํ๋ก ํธ์๋ ์ํ ๊ด๋ฆฌ ์ญํ ์ ๋ด๋นํ๋ค.
import { signInWithEmailAndPassword } from 'firebase/auth';
import { auth } from './firebase'; // ์ด๊ธฐํ๋ Firebase auth ์ธ์คํด์ค
export const loginWithFirebase = async (email, password) => {
const userCredential = await signInWithEmailAndPassword(auth, email, password);
return userCredential.user;
};
// actions/userActions.ts
export const setUser = (user) => ({
type: 'SET_USER',
payload: user,
});
// reducers/userReducer.ts
const initialState = {
currentUser: null,
};
export default function userReducer(state = initialState, action) {
switch (action.type) {
case 'SET_USER':
return { ...state, currentUser: action.payload };
default:
return state;
}
}
const handleLogin = async () => {
const user = await loginWithFirebase(email, password);
dispatch(setUser(user));
};
์ด๋ฐ ์์ผ๋ก Firebase์์ ๋ฐ์ ์ฌ์ฉ์ ์ ๋ณด๋ฅผ Redux์ ์ ์ฅํด ์ ์ญ์์ ์ฌ์ฉํ ์ ์๊ฒ ๋ง๋ค ์ ์๋ค.
onAuthStateChanged์ ๊ฐ์ ๋ฆฌ์ค๋๋ ์์ผ๋ฏ๋ก,onSnapshot๊ณผ Redux์ dispatch๋ฅผ ํจ๊ป ํ์ฉํ๋ฉด ์ข๋ค.