数据在next.js中变得重复

  • 本文关键字:next js 数据 api next.js
  • 更新时间 :
  • 英文 :


我有数据从API。我需要模态删除和编辑它。但是当我打开模态窗口时,刷新

后数据被复制
const [category, setCategory] = useState()
let myValue = []
const getCategory = async () => {
const id = window.localStorage.getItem('categoryId')
await axios.get(`${API_URL}/categories/${id}`, {
headers: {
Authorization: token,
Accept: 'application/json'
},
})
.then(res => {
const category = res.data
console.log(category.data, 'shu');
myValue.push(category.data)
console.log(myValue, 'value');
setCategory(myValue)
})
.catch(err => console.log(err))
}

这是获取数据的代码。这是情态动词的代码。

const [openDelete, setOpenDelete] = useState(-1);
const handleOpenDelete = (elem) => {
setOpenDelete(elem.id);
// console.log(index);
}
const handleCloseDelete = () => {
setOpenDelete(-1);
}

不需要将数据推送到myValue数组。与其设置myValue.push(category.data),不如直接设置setCategory(category.data)

最新更新