Javascript 上下文Api firestore
我创建了一个上下文,以便能够在Firestore中Crud我的Categories集合,但是我找不到一种方法来关注getCategories()函数中的更改。起初创建了一个无限循环,但经过几次尝试后,我能够修复它。现在,在上下文中,如果您正确调用firestore数据,但希望将其发送到我使用上下文的组件,则无法读取数据 函数getCategories in context.jsJavascript 上下文Api firestore,javascript,reactjs,firebase,react-native,google-cloud-firestore,Javascript,Reactjs,Firebase,React Native,Google Cloud Firestore,我创建了一个上下文,以便能够在Firestore中Crud我的Categories集合,但是我找不到一种方法来关注getCategories()函数中的更改。起初创建了一个无限循环,但经过几次尝试后,我能够修复它。现在,在上下文中,如果您正确调用firestore数据,但希望将其发送到我使用上下文的组件,则无法读取数据 函数getCategories in context.js . . . const getCategories =
.
.
.
const getCategories = (dispatch) => async () => {
return ref.onSnapshot((querySnapshot) => {
const data = []
querySnapshot.forEach(doc => {
const { name } = doc.data()
data.push({
id: doc.id,
name
})
})
console.log('context ',data) //prints correctly
dispatch({
type: 'getCategories',
payload: data
});
});
}
.
.
.
Home.js
.
.
.
const { categoriesState, getCategories } = useContext(CategoriesContext);
const { categories } = categoriesState;
const [categoriesData, setCategoriesData] = useState(categories);
useEffect(() => {
console.log('Cate statee: ', categories) //print [] which is the initial state of the context
getCategories()
console.log('Cate stateHome : ', categoriesData) //print [] which is the initial state of the context
}, []);
.
.
.