gpt4 book ai didi

javascript - React.js函数组件useState相关问题

转载 作者:行者123 更新时间:2023-11-30 19:01:15 24 4
gpt4 key购买 nike

我想访问状态数据。

如何将我拥有的数组对象复制到数据数组中?

enter image description here

-代码

function TableList() {

const [state, setState] = React.useState({
columns: [
{ title: '로트번호', field: 'lote_id' },
{ title: '중량(kg)', field: 'item_weight' },
{ title: '수량', field: 'item_quantity' },
{ title: '제품코드', field: 'item_code' },
{ title: '제품명', field: 'item_name' },
{ title: '규격', field: 'standard' },
{ title: '재질', field: 'material' },
{ title: '공정명', field: 'process_name' },
{ title: '단중', field: 'unitweight' },
{ title: '납기일시', field: 'duedate' },
{ title: '단가', field: 'item_cost' },
{ title: '금액', field: 'item_price' },
],
data: [],
});


useEffect(() =>{
console.log("실행")
console.log(state.data);

axios.get('http://localhost:8080/api/item/1')
.then( response => {
//I want to copy the data of my output.list to state.data.
})
.catch( response => {
console.log(response);
})
});

我想将我的 axios 响应数据复制到声明为 useState 的数据数组中。请告诉我最有效的方法。

  1. 如何访问第二个参数 state.data?

  2. 我想访问并放置我的 output.list 响应数据。

最佳答案

不完全清楚你在找什么,但如果你只想更新状态对象上的 data 属性,你可以这样做:

const arrayOfObjects = [{ id: 1 }, { id: 2 }];
const newState = Object.assign({}, state);
newState.data = arrayOfObjects;
setState(newState);

关于javascript - React.js函数组件useState相关问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59494756/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com