【发布时间】:2022-11-04 19:40:05
【问题描述】:
我在从 API 填充数据时遇到一些问题。当我 console.log 状态“dataFromApi”时,它工作得很好。意思是,我得到了多个对象的 arr 。
但是,我随后将状态中的 API 数据插入“columnsFromBackend”、“items”部分。然后,当我控制台记录页面底部的“columns”状态时,它只是来自“columnsFromBackend”的所有数据,它返回所有 hardCodedData,但不是来自 API 的数据。
意思是,我只是收到一个空数组。这是 console.log(columns) 的输出。关于这里可能发生的事情有什么建议吗?
const [dataFromApi, setDataFromApi] = useState([]);
useEffect(() => {
getLeadsClApproved().then((resp) => {
setDataFromApi(resp.data);
});
}, []);
const hardCodedData = [
{
id: uuid(),
business_name: "Canva",
first_name: "Melanie",
last_name: "Perkins",
created_at: "15th of Nov., 2022",
},
{
id: uuid(),
business_name: "Microsoft",
first_name: "Bill",
last_name: "Gates",
created_at: "15th of Nov., 2022",
},
];
const columnsFromBackend = {
[uuid()]: {
name: "In Progress",
items: hardCodedData,
},
[uuid()]: {
name: "CL Approved",
items: dataFromApi,
},
[uuid()]: {
name: "CL Declined",
items: [],
},
[uuid()]: {
name: "Awaiting Response",
items: [],
},
[uuid()]: {
name: "Interview Scheduled",
items: [],
},
[uuid()]: {
name: "Accepted",
items: [],
},
[uuid()]: {
name: "Rejected",
items: [],
},
};
const [columns, setColumns] = useState(columnsFromBackend);
console.log(columns); // logs the columns with its content
【问题讨论】:
标签: javascript node.js reactjs express