【发布时间】:2020-08-31 17:17:04
【问题描述】:
我已成功从 firestore 检索数据,但无法更新我的状态。 请检查代码。
export const TaskContextProvider = (props) => {
const [Tasks, setTasks] = useState([])
const { User } = useContext(AuthContext)
useEffect(() => {
firebase.firestore()
.collection("Users")
.doc(User.id)
.collection("Tasks")
.onSnapshot((snapshot) => {
const newtask = snapshot.docs.map((doc)=>({
id : doc.id,
...doc.data()
}))
setTasks(newtask)
console.log('newtask', newtask)
console.log('Tasks', Tasks)
})
}, [])
下面的控制台输出是
newtask (10) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
Tasks []
【问题讨论】:
-
这能回答你的问题吗? Reactjs state not updating
标签: reactjs firebase google-cloud-firestore react-hooks