我正在使用 useEffect
将数据快照从我的 firebase 数据库导入我的应用程序,如下所示:
const [client, setClient] = useState([]);
const { user } = useContext(AuthContext);
const clientRef = firebase.database().ref("users").child(user.uid).child("invoices");
useEffect(() => {
clientRef
.on('value', (snapshot) => {
const invs =[];
snapshot.forEach(function(willSnapshot) {
wills.push(willSnapshot.key);
})
setClient(invs);
},
error => {
console.log(error)
}
)
}, [])
完成发票结算后,用户可以删除发票:
const deleteItem = (string) => {
let allItems = [...client];
let filteredItems = allItems.filter((item) => item != string);
const db = firebase.database().ref("users").child(user.uid).child("wills").child(string);
Alert.alert(
"Confirm Delete",
"Are you sure you want to delete this document",
[
{
text: "Cancel",
onPress: () => console.log("Cancel Pressed"),
style: "cancel"
},
{ text: "OK", onPress: () => {setClient(filteredItems); db.remove()} }
]
);
当调用 deleteItem
函数的按钮被按下时,我收到错误消息:
Can't perform a React state update on an unmounted component
有没有办法在我的代码中解决这个问题?我厌倦了使用 clientRef.off("Value")
但它没有用。