【发布时间】:2022-10-01 07:28:19
【问题描述】:
我正在尝试创建一个 Web 应用程序,该应用程序在用户键入信息时生成简历。
我无法通过 input/handleChange 更改组件状态下对象的属性。我知道我可以通过使用输入的名称作为对我要更改的属性的引用来更改状态对象属性,但是当组件的状态中有多个对象时我不知道如何执行此操作.
我有一个按钮,可以将对象添加到 schoolData 状态并将组件添加到 schoolArray 状态。
输入组件:
import React from \'react\';
import Schools from \'./Schools\';
export default function Input(props) {
// school data and event handler
const [schoolData, setSchoolData] = React.useState(
[{
schoolName: \'\', schoolState: \'\', schoolCity: \'\', schoolDegree: \'\', schoolStartDate: \'\', schoolEndDate: \'\', schoolCurrent: false,
}]
);
function handleSchoolChange(event) {
const { name, value, type, checked } = event.target;
setSchoolData(prevData => ({
...prevData,
[name]: type === \"checkbox\" ? checked : value
}));
console.log(\'school data\', schoolData);
console.log(\'school data name\', schoolData.name);
console.log(\'school array\', schoolArray);
}
// school array and event handler
const [schoolArray, setSchoolArray] = React.useState([
<Schools key={schoolData.length} schoolData={schoolData} handleSchoolChange={handleSchoolChange} />,
]);
function addSchool() {
// add schoolData object
setSchoolData(prevData => [...prevData, {
schoolName: \'\', schoolState: \'\', schoolCity: \'\', schoolDegree: \'\', schoolStartDate: \'\', schoolEndDate: \'\', schoolCurrent: false,
}]);
setSchoolArray(prevSchoolArray => [...prevSchoolArray, <Schools key={schoolData.length + 1} schoolData={schoolData[0]} handleSchoolChange={handleSchoolChange} />]);
console.log(schoolArray);
console.log(schoolElements);
console.log(\'school name\', schoolData[0].schoolName);
}
const schoolElements = schoolArray.map((school, index) =>
{ return school }
);
return (
<form>
<legend>Education</legend>
{schoolElements}
</form>
)
}
学校组成:
export default function Schools(props) {
return (
<div>
<label htmlFor=\"schoolName\">School Name</label>
<input
type=\"text\"
placeholder=\"School Name\"
className=\"input\"
name=\"schoolName\"
onChange={props.handleSchoolChange}
value={props.schoolData.schoolName}
/>
<label htmlFor=\"schoolCity\">City</label>
<input
type=\"text\"
placeholder=\"City\"
className=\"input\"
name=\"schoolCity\"
onChange={props.handleSchoolChange}
value={props.schoolData.schoolCity}
/>
<label htmlFor=\"schoolState\">State</label>
<input
type=\"text\"
placeholder=\"State\"
className=\"input\"
name=\"schoolState\"
onChange={props.handleSchoolChange}
value={props.schoolData.schoolState}
/>
<label htmlFor=\"schoolDegree\">Degree</label>
<input
type=\"text\"
placeholder=\"Degree\"
className=\"input\"
name=\"schoolDegree\"
onChange={props.handleSchoolChange}
value={props.schoolData.schoolDegree}
/>
<label htmlFor=\"schoolStartDate\">Start Date</label>
<input
type=\"month\"
placeholder=\"Start Date\"
className=\"input\"
name=\"schoolStartDate\"
onChange={props.handleSchoolChange}
value={props.schoolData.schoolStartDate}
/>
<label htmlFor=\"schoolEndDate\">End Date</label>
<input
type=\"month\"
placeholder=\"End Date\"
className=\"input\"
name=\"schoolEndDate\"
onChange={props.handleSchoolChange}
value={props.schoolData.schoolEndDate}
/>
<label htmlFor=\"schoolCurrent\">Still attending</label>
<input
type=\"checkbox\"
name=\"schoolCurrent\"
onChange={props.handleSchoolChange}
checked={props.schoolData.schoolCurrent}
/>
</div>
);
}
提前感谢您的帮助!