[英]ReactJS Form: Parent Component state updates, but Child's field gets emptied & props remain unchanged
我已經為動態表單編寫了ReactJS代碼來收集學生的詳細信息。 可以添加或刪除任意數量的學生。 父組件是App ,子組件是StudentsFormElement ,它呈現一個學生輸入字段。 添加和刪除字段工作得很好。
問題:我不能在表單字段中輸入超過一個字符,並且在輸入后它們會立即清除。 使用 DevTools,我可以看到鍵入的字符反映在父組件的狀態中,但更改未能通過道具向下傳播到子組件。 因此,DOM 清除到原始狀態。 (注意:我發現了幾個相似的問題,但沒有一個能回答這個特定的問題)。 忽略 onStudentsChange() 中被注釋掉的 else 條件。 它處理添加和刪除學生。
代碼和盒子: https ://codesandbox.io/embed/inspiring-kare-qb9hz
應用程序(父):
class App extends React.Component {
constructor(props) {
super(props)
let studentsFormElementsTemp = []
let tempSTUDENTS = {0: ["", ""]}
this.state = {
STUDENTS: tempSTUDENTS
}
studentsFormElementsTemp.push(<StudentsFormElement id="0" student={this.state.STUDENTS[0]} onStudentsChange={this.onStudentsChange} />)
this.state = {
studentsFormElements: studentsFormElementsTemp,
studentsElementsIdArray: [0],
STUDENTS: tempSTUDENTS
}
}
onStudentsChange = (e) => {
if (e.target.name === "studentId" || e.target.name === "studentName") {
let tempSTUDENTS = this.state.STUDENTS
if (e.target.name === "studentId") {
tempSTUDENTS[e.target.id][0] = e.target.value
}
else {
tempSTUDENTS[e.target.id][1] = e.target.value
}
this.setState({
STUDENTS: tempSTUDENTS
})
} else { //IGNORE. DEALS WITH 'X' and '+' BUTTONS.
/*let studentsFormElementsTemp = this.state.studentsFormElements
let studentsElementsIdArrayTemp = this.state.studentsElementsIdArray
let tempSTUDENTS = this.state.STUDENTS
if (e.target.id === "+") {
tempSTUDENTS[studentsElementsIdArrayTemp[studentsElementsIdArrayTemp.length - 1] + 1] = ["", ""]
this.setState({
STUDENTS: tempSTUDENTS
})
studentsFormElementsTemp.push(<StudentsFormElement id={studentsElementsIdArrayTemp[studentsElementsIdArrayTemp.length - 1] + 1} student={this.state.STUDENTS[studentsElementsIdArrayTemp[studentsElementsIdArrayTemp.length - 1] + 1]} onStudentsChange={this.onStudentsChange} />)
studentsElementsIdArrayTemp.push(studentsElementsIdArrayTemp[studentsElementsIdArrayTemp.length - 1] + 1)
this.setState({
studentsFormElements: studentsFormElementsTemp,
studentsElementsIdArray: studentsElementsIdArrayTemp
})
} else {
let studentIndex = studentsElementsIdArrayTemp.indexOf(parseInt(e.target.id))
studentsFormElementsTemp.splice(studentIndex, 1)
studentsElementsIdArrayTemp.splice(studentIndex, 1)
delete tempSTUDENTS[e.target.id]
this.setState({
studentsFormElements: studentsFormElementsTemp,
studentsElementsIdArray: studentsElementsIdArrayTemp,
STUDENTS: tempSTUDENTS
})
}
*/}
}
render() {
return (
<div>
<h2 style={{textAlign: "center", display: "inline-block"}}>Students</h2><Button id="+" style={{display: "inline-block"}} variant="success" onClick={this.onStudentsChange}>+</Button>
<form>
{this.state.studentsFormElements}
</form>
</div>
)
}
}
StudentsFormElement(子):
class StudentsFormElement extends React.Component {
render() {
return (
<InputGroup className="mb-3">
<FormControl name="studentId" id={this.props.id} value={this.props.student[0]} placeholder="Id" onChange={this.props.onStudentsChange} />
<FormControl name="studentName" id={this.props.id} value={this.props.student[1]} placeholder="Name" onChange={this.props.onStudentsChange} />
<InputGroup.Append style={{display: "inline-block"}}>
<Button id={this.props.id} variant="danger" onClick={this.props.onStudentsChange}>X</Button>
</InputGroup.Append>
</InputGroup>
)
}
}
這是一個只有一個學生字段且沒有添加/刪除按鈕的版本。 由於我直接在父組件的 render() 中使用了子組件,因此鍵入有效。 但是我需要讓它與上面的代碼一樣與多個學生領域一起工作。 https://codesandbox.io/s/quirky-colden-gkiz6
起初,可能在 FormControl 字段中該值未定義,因為 this.props.student[0] 未定義在 FormControl 中刪除 value 屬性並在 defaultValue 屬性中分配 props 值
<FormControl name="studentId" id={this.props.id} defaultValue={this.props.student[0]} placeholder="Id" onChange={this.props.onStudentsChange} />
<FormControl name="studentName" id={this.props.id} defaultValue={this.props.student[1]} placeholder="Name" onChange={this.props.onStudentsChange} />
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.