import { initializeApp } from 'firebase/app';
import { getAuth } from 'firebase/auth';
import { getFirestore } from 'firebase/firestore'; // import
const firebaseConfig = {
...
};
export const firebase = initializeApp(firebaseConfig);
export const auth = getAuth();
export const db = getFirestore(firebase); // db ์์ฑ
Cloud Firestore ๋ฐ์ดํฐ๋ฒ ์ด์ค ๋ง๋ค๊ธฐ - Firebase
import { collection, addDoc } from 'firebase/firestore';
import { db } from 'fbase';
const Home = () => {
const [tweet, setTweet] = useState('');
// input ํธ๋ค๋ฌ
const onChange = (e) => {
const { target: { value } } = e;
setTweet(value); // setTweet(e.target.value);
};
// ํธ์ ์ถ๊ฐ
const onSubmit = async (e) => {
e.preventDefault();
try {
const docRef = await addDoc(collection(db, 'Tweets'), {
text: tweet,
createdAt: Date.now(),
});
console.log('Document written with ID: ', docRef.id);
} catch (error) {
console.error('Error adding document: ', error);
}
setTweet('');
};
return (
<>
Home
<form onSubmit={onSubmit}>
<input value={tweet} onChange={onChange} type='text' placeholder="What's happening?" maxLength={120} />
<input type='submit' value='Tweet' />
</form>
</>
);
};
์ด์ ํธ์์ ์์ฑํ๋ฉด Firebase ์ฝ์์์ ์๋์ฒ๋ผ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ถ๊ฐ๋๋ ๊ฒ์ ํ์ธํ ์ ์๋ค.
import { db } from 'fbase';
import { collection, getDocs } from 'firebase/firestore';
const Home = ({ userObj }) => {
const [tweets, setTweets] = useState([]);
useEffect(() => {
getTweets();
}, []);
// ํธ์ ๋ถ๋ฌ์ค๊ธฐ
const getTweets = async () => {
const querySnapshot = await getDocs(collection(db, 'Tweets'));
querySnapshot.forEach((doc) => {
const tweetObject = {
...doc.data(), // {createdAt : ..., text: ...}
id: doc.id, // id ์ถ๊ฐ
creatorId: userObj.uid, // ์์ฑ์ id ์ถ๊ฐ
};
setTweets((prev) => [tweetObject, ...prev]); // ์๋ก์ด ํธ์ ๋งจ ์์ ์ถ๊ฐ
});
};
return (
<>
Home
<div>
{tweets.map((tweet) => {
return (
<div key={tweet.id}>
<h4>{tweet.text}</h4>
</div>
);
})}
</div>
</>
);
};
์ปฌ๋ ์ ์ ๋ชจ๋ ๋ฌธ์ ๊ฐ์ ธ์ค๊ธฐ - Firebase
// ์ค์๊ฐ์ผ๋ก ๋ฐ์ดํฐ๋ฅผ ๋ฐ์ดํฐ๋ฒ ์ด์ค์์ ๊ฐ์ ธ์ค๊ธฐ
useEffect(() => {
const q = query(collection(db, 'Tweets'), orderBy('createdAt', 'desc'));
const unsubscribe = onSnapshot(q, (querySnapshot) => {
const newArr = querySnapshot.docs.map((doc) => {
return {
...doc.data(),
id: doc.id,
creatorId: userObj.uid,
};
});
setTweets(newArr);
});
}, []);
์ปฌ๋ ์ ์ ์ฌ๋ฌ ๋ฌธ์ ๋ฃ๊ธฐ
3๋ฒ์์ ์์ฑํ ์ฝ๋๋ฅผ ์์ฒ๋ผ ๋ฐ๊ฟ์ฃผ๋ฉด, ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ ๋ฐ์ดํธ๊ฐ ์๊ธธ ๋๋ง๋ค ์๋ก๊ณ ์นจํ์ง ์์๋ ๋ฐ๋ก ํ๋ฉด์ ๋ ๋๋ง๋๋ค.
deleteDoc
ํจ์์ ๋ฐ์ดํฐ๋ฒ ์ด์ค, collection๋ช
, ์ญ์ ํ๋ ค๋ document๋ช
์ ํจ๊ป ์ ๋ฌํด์ผ ํ๋ค.
import { doc, deleteDoc } from 'firebase/firestore';
import { db } from 'fbase';
const Tweet = ({ tweetObj, isCreator }) => {
// ํธ์ ์ญ์
const onDeleteClick = async () => {
const ok = window.confirm('์ด ํธ์์ ์ญ์ ํ์๊ฒ ์ต๋๊น?');
if (ok) {
await deleteDoc(doc(db, 'Tweets', tweetObj.id));
}
};
return (
<div key={tweetObj.id}>
<h4>{tweetObj.text}</h4>
// ํธ์ ์์ฑ์๋ง(isCreator === true์ผ ๋) ์ญ์ , ์์ ๋ฒํผ์ด ๋ณด์ธ๋ค.
{isCreator && (
<>
<button onClick={onDeleteClick}>Delete</button>
<button>Edit</button>
</>
)}
</div>
import { doc, updateDoc } from 'firebase/firestore';
import { db } from 'fbase';
const Tweet = ({ tweetObj, isCreator }) => {
const [isEditing, setIsEditing] = useState(false); // ํธ์ง ๋ชจ๋
const [newTweet, setNewTweet] = useState(tweetObj.text); // input
const newTweetRef = doc(db, 'Tweets', tweetObj.id);
const toggleEditing = () => setIsEditing((prev) => !prev);
// input ํธ๋ค๋ฌ
const onChange = (e) => {
const {
target: { value },
} = e;
setNewTweet(value);
};
// ํธ์ ์์
const onSubmit = async (e) => {
e.preventDefault();
await updateDoc(newTweetRef, {
text: newTweet,
});
setIsEditing(false);
};
return (
<div key={tweetObj.id}>
{isEditing ? (
<>
<form onSubmit={onSubmit}>
<input value={newTweet} onChange={onChange} type='text' placeholder='Edit your tweet' required />
<input type='submit' value='update' />
</form>
<button onClick={toggleEditing}>cancel</button>
</>
) : (
<>
<h4>{tweetObj.text}</h4>
{isCreator && (
<>
<button onClick={onDeleteClick}>Delete</button>
<button onClick={toggleEditing}>Edit</button>
</>
)}
</>
)}
</div>
);
};
๋ฌธ์ ์ ๋ฐ์ดํธ - Firebase
ํ์ง๋ง ์ฌ๊ธฐ์ ๋ง์ฝ ๋ฐ์ดํฐ๊ฐ ์ถ๊ฐ๋ ๋๋ง ์ค์๊ฐ์ผ๋ก ๋ณด๊ณ ์ถ๊ณ ,
๋ฐ์ดํฐ๊ฐ ์ญ์ , ์์ ๋๋ ๊ฒฝ์ฐ๋ ์ค์๊ฐ์ผ๋ก ๋ณด๊ณ ์ถ์ง ์๋ค๋ฉด ์ด๋ป๊ฒ ํด์ผํ ๊น?