我有数据从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)
。