我正在制作一个使用 firestore 数据库进行存储的 react/redux 应用程序,并且我能够将我的应用程序链接到 firestore,以便我可以从我的应用程序中创建、读取、更新和销毁新数据到 firestore。问题是,在我的一个页面上,当我更新数据时,它会立即更新后端 Firestore 数据库中的数据,但该更改不会立即反映在页面上的屏幕上。相反,它仅在我对页面进行硬刷新后才会显示。此外,我目前正在使用https://github.com/prescottprue/react-redux-firebase和https://github.com/prescottprue/redux-firestore在我的 react/redux 应用程序中使用 firebase。
那么,我需要做些什么才能使我的页面自动加载新的更改,因为我认为使用 react-firestore 的“firestoreConnect”会起作用,但事实并非如此。谢谢大家,这是我的代码:
数据组件
import React, { Component } from "react";
import { connect } from "react-redux";
import { firestoreConnect, isLoaded } from "react-redux-firebase";
import { compose } from "redux";
import { Grid } from "semantic-ui-react";
import BudgetHeader from "../BudgetHeader/BudgetHeader";
import BudgetList from "../BudgetList/BudgetList";
import BudgetNav from "../BudgetNav/BudgetNav";
import { updateBudgetData, compareChooser } from "../budgetActions";
import LoadingComponent from "../../../app/layout/LoadingComponent";
const mapState = state => ({
users: state.firestore.ordered.users
});
const actions = {
updateBudgetData,
compareChooser
};
class BudgetDashboard extends Component {
updateBudgetData = (newData, fieldToUpdate) => {
this.props.updateBudgetData(newData, fieldToUpdate);
};
onCompareChange = (event, data) => {
if (data.checked) {
this.props.compareChooser("net");
} else {
this.props.compareChooser("gross");
}
};
render() {
const { users } = this.props;
if (!isLoaded(users)) return <LoadingComponent inverted={true} />;
return (
<Grid>
<Grid.Column width={16}>
<BudgetNav />
<BudgetHeader
general={users[0].budget[1]}
onCompareChange={this.onCompareChange}
/>
<BudgetList
className="standardSeparation"
data={users[0].budget[0]}
unallocatedBudget={users[0].budget[1].unallocatedBudget}
unallocatedSpending={users[0].budget[1].unallocatedSpent}
updateData={this.updateBudgetData}
/>
</Grid.Column>
</Grid>
);
}
}
export default compose(
connect(
mapState,
actions
),
firestoreConnect([
{
collection: "users",
doc: "asdfasdfF21871d",
subcollections: [{ collection: "budget" }]
}
])
)(BudgetDashboard);
组件操作(更新预算方法所在的位置)
export const updateBudgetData = (inputData, fieldToUpdate) => {
return async (dispatch, getState, { getFirebase, getFirestore }) => {
try {
const firestore = getFirestore();
// Get all firestore data nodes that will be used for updating after data change
const userBudget = firestore
.collection("users")
.doc("asdfasdfas8128fsa")
.collection("budget");
const userDataRef = userBudget.doc("data");
const userGeneralRef = userBudget.doc("general");
// variables to be used in generating updated maps
var grossIncome = 0;
var netIncome = 0;
var userDataMap = {};
var userGeneralMap = {};
// get grid data
await userDataRef
.get()
.then(function(doc) {
if (doc.exists) {
userDataMap = doc.data();
} else {
console.log("No user data");
}
})
.catch(function(error) {
console.log("User Data Document Get Error: ", error);
});
// get header data
await userGeneralRef
.get()
.then(function(doc) {
if (doc.exists) {
const data = doc.data();
userGeneralMap = data;
grossIncome = data.grossIncome;
netIncome = data.netIncome;
} else {
console.log("No general data");
}
})
.catch(function(error) {
console.log("User General Document Get Error: ", error);
});
// generate new data and general document mappings
const updatedMaps = generateUpdatedMaps(
userDataMap,
userGeneralMap,
grossIncome,
netIncome,
inputData,
fieldToUpdate
);
const newUserDataMap = updatedMaps.newDataMap;
const newUserGeneralMap = updatedMaps.newGeneralMap;
// update firestore data with new mappings
userDataRef.set(newUserDataMap);
userGeneralRef.set(newUserGeneralMap);
toastr.success("Success!", "Budget data has been updated");
} catch (error) {
toastr.error("Oops", "Something went wrong");
}
};
};
我认为通过将我的 BudgetDashboard 组件包装在 firestoreConnect 中,监听器会在发生更改时自动更新,但这并没有发生。相反,只有在硬刷新时,我的 LoadingComponent 才会显示,新数据最终会反映在屏幕上。那么如何让任何更改立即反映屏幕上的更改?