我有预订应用程序.如果用户点击预订详细信息BTN,用户可以看到数据.已提取数据,但useEffect不满意.
UseEffect向我显示此错误消息: Reaction Hook React.useEffect缺少依赖项:‘getDetail’.包括它或删除依赖项array.
我不能删除依赖数组,它给出了无限循环,对吗?那么,如何解决这个问题呢? 我试着把我所有的东西都放在那里,但还是有黄色下划线.
const BookedDetails = () => {
const { user } = useSelector((state) => state.userAuth)
const userID = user?.user?._id
const [details, setDetails] = React.useState([])
const [loading, setLoading] = React.useState(false)
const [invoiceLoad, setInvoiceLoad] = React.useState(false)
const [currentPage, setCurrentPage] = React.useState(1)
const [postPerPage, setPostPerPage] = React.useState(4)
const indexOfLastPost = currentPage * postPerPage
const indexOfFirstPost = indexOfLastPost - postPerPage
const currenPosts = details.slice(indexOfFirstPost, indexOfLastPost)
const pages = []
for(let i = 1; i <= Math.ceil(details.length / postPerPage); i++) {
pages.push(i)
}
const paginate = (x) => {
setCurrentPage(x)
}
const getDetails = async () => {
const config = {
headers: {
"Content-Type": "application/json",
}
}
try {
setLoading(true)
const res = await axios.post('/api/bookingDetails/bookDetails', { userID }, config )
//console.log(res.data.user)
setDetails(res.data.user)
setLoading(false)
} catch (error) {
console.log(error.message)
setLoading(false)
}
}
React.useEffect(() => {
getDetails()
},[ ]) // << dependency is yellow underlined.