我正在做的事情出了什么问题,我正在获取集合快照,将其循环遍历并将其推入状态,然后使用该状态呈现一些列表项
但我得到那个错误:
Unhandled Rejection (TypeError): Cannot read property 'map' of undefined
代码:
const [books, setBooks] = useState([]);
db.collection('books').get()
.then(snapshot => snapshot.forEach(doc => {
setBooks([...books, doc.data()])
}))
return (
books.map(return some jsx)
)
答案 0 :(得分:1)
您做的setState
过多。为什么不只做一次呢?
const [books, setBooks] = useState([]);
db.collection('books')
.get()
.then(snapshot => snapshot.map(s => s.data())
.then(data => setBooks(data))
}))
return (
books.map(b => <div>{book stuff here}</div>)
)