我正在从Firestore中获取数据,并且在页面加载时需要执行一次。当我使用useState时,一切都很好:
const [data, setData] = useState([]);
useEffect(()=> {
db.collection("articles").orderBy("date", "desc").get()
.then(snapshot => {
const tempArticles = [];
snapshot.docs.forEach(doc => tempArticles.push({...doc.data(), id: doc.id}))
setData(tempArticles)
})
}, [])
没有警告,一切都在做应该做的事情。 现在我想全局访问数据,所以我使用useContext而不是useState:
const {data, setData} = useContext(ArticleContext);
useEffect(()=> {
db.collection("articles").orderBy("date", "desc").get()
.then(snapshot => {
const tempArticles = [];
snapshot.docs.forEach(doc => tempArticles.push({...doc.data(), id: doc.id}))
setData(tempArticles)
})
}, [])
现在我有警告
React Hook useEffect缺少依赖项:'setData'。包括它或删除依赖项数组