[英]How to push elements into array in react state, the array is located inside of object
将 object 推入数据中的问题数组中,它会给出此错误 Uncaught TypeError: data.sections is not iterable at handleClick (SecondStep.tsx:14:1)
export default function CreateStepper() { const [data, setData] = useState({ name: "", desc: "", sections: [], }); console.log(data); return ( <><SecondStep data={data} setData={setData} /></>) } function SecondStep(data: any, setData: any) { const [addSection, setAddSection] = useState(1); const newSection = { sectionName: "Hdsd", sectionDesc: "dsdsd", question: [], }; const handleClick = () => { setData({...data, sections: [...data.sections, newSection] }); }; return ( <Box> </Box> ); } export default SecondStep;
当我执行下面的代码时,它会返回“Uncaught TypeError: Cannot read properties of undefined (reading 'push')”这个类型错误。 如何将新的 object 推送到 React useState 中的数组。 首先useState是object里面的object有疑问:【数组如何推入object这个数组】
const [data, setData] = useState({ name: "", desc: "", sections: [], }); const newSection = { sectionName: "Hello", sectionDesc: "Desc", question: [], }; const handleClick = () => { let copyData = data; copyData.sections.push(newSection); setData(copyData); };
您可以在 setState 方法中获取最新版本的 state,然后您可以在其中添加您的 object。
const [data, setData] = useState({
name: "",
desc: "",
sections: [],
});
const newSection = {
sectionName: "Hello",
sectionDesc: "Desc",
question: [],
};
const handleClick = () => {
setData(data => ({
...data,
sections: [...data.sections, newSection]
})
};
但我实际上不太确定您是否需要...data
,您可以将其排除在外。
您必须克隆 object 然后将元素推送到数组中,
这是下面的示例,
import "./styles.css";
import React, {useState} from 'react'
export default function App() {
const [data, setData] = useState({
name: "",
desc: "",
sections: [],
});
const newSection = {
sectionName: "Hello",
sectionDesc: "Desc",
question: [],
};
const handleClick = () => {
setData({...data, sections:[...data.sections, newSection]});
};
return (
<div className="App">
<h1>Hello CodeSandbox</h1>
<button onClick={handleClick}>Click</button>
<h4>Sections: {data.sections[0]?.sectionName}</h4>
</div>
);
}
代码沙箱: https://codesandbox.io/s/react-do7bk?file=/src/App.js:0-572
更新问题的答案是:
import "./styles.css";
import React, { useState } from "react";
export const CreateStepper = () => {
const [data, setData] = useState({
name: "",
desc: "",
sections: []
});
const SecondStep = (data: any) => {
const [addSection, setAddSection] = useState(1);
const newSection = {
sectionName: "Hdsd",
sectionDesc: "dsdsd",
question: []
};
const handleClick = () => {
setData({ ...data, sections: [...data.sections, newSection] });
};
return <></>;
};
return (
<>
<SecondStep data={data} />
</>
);
};
export default CreateStepper;
您还可以通过道具为 SecondStep 提供 function 如果您愿意,这应该可以:
export default function CreateStepper() {
const [data, setData] = useState({
name: "",
desc: "",
sections: [],
});
console.log(data);
return (
<><SecondStep data={data} setData={setData} /></>)
}
function SecondStep(data: any, setData: (data : any) => void) {
const [addSection, setAddSection] = useState(1);
const newSection = {
sectionName: "Hdsd",
sectionDesc: "dsdsd",
question: [],
};
const handleClick = () => {
props.setData({ ...data, sections: [...data.sections, newSection] });
};
return (
<Box>
</Box>
);
}
export default SecondStep;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.