当item被插入到React js Array状态时,数据正在被复制,
console.log(newData)
输出单个对象,而
console.log(data)
输出连续重复
console.log(newData)
我该怎么修
const [data, setData] = useState([])
const addData = (newData) => {
console.log(newData)
setData(prevData => [...prevData, newData]);
console.log(data)
};
useEffect(() => {
axios
.get(`http://localhost:3000/api/products/${pid}`)
.then((res) => {
addData(res.data); // Update the state directly with the response data
})
.catch((err) => {
console.log(err);
});
});
2条答案
按热度按时间n3ipq98p1#
gc0ot86w2#