【发布时间】:2021-08-29 07:31:05
【问题描述】:
我有一个反应组件,它将在安装时创建一个新文档
const CreateGame: React.FunctionComponent<ICreateGameProps> = (props) => {
const gamesRef = useFirestore()
.collection('Games')
const [newGameId, setNewGameId] = useState('')
useEffect(() => {
const newGame: IGameDoc = {
playerTurn: 'x',
secondPlayerJoined: false,
gameState: {
rowOne: [null, null, null],
rowTwo: [null, null, null],
rowThree: [null, null, null]
}
}
gamesRef.add(newGame)
.then(docRef => setNewGameId(docRef.id))
return () => {
gamesRef.doc(newGameId).delete()
}
}, [])
但是,一旦组件再次卸载,我想再次删除同一个文档,因此我的 useEffect 挂钩中有清理功能
return () => {
gamesRef.doc(newGameId).delete()
}
但这不起作用。有谁知道为什么?
【问题讨论】:
标签: reactjs firebase react-hooks use-effect