当我应用useEffect
钩子时,这个函数遇到了一些问题。有人知道我该怎么解决吗?
const [expenses, setExpenses] = UseState([])
const getItems = async() => {
try {
// Right now we are receiving all the expenses and filtering by the username but we should only receive the one from the username --> that will be modified
const data = await axios.get('https://controladorgastosapi.herokuapp.com/expenses/', {
headers: {
username: user.userInfo.username
}
})
const result = data.data
setExpenses(result)
} catch (err) {
console.log(err)
}
}
useEffect(() => {
getItems()
}, [expenses])
您希望将值设置为页面加载时的状态。您应该删除此引号内的值";[]";
useEffect(() => {
getItems()
}, [])
说明:当这些引号中指定的状态发生更改时,useEffect将再次工作。";费用";state有一个空数组的默认值,useEffect对此作出响应,调用";getItems"函数,它将一个值分配给";费用";状态等等。