【发布时间】:2020-05-13 10:46:16
【问题描述】:
我正在成功地动态创建受控的 TextInputs。对于创建的每个 TextInput,还会创建一个“删除”按钮,该按钮在触发时应从数组中删除该特定的 TextInput。但是,它总是删除数组中的最后一个 TextInput。这是父组件中定义的删除函数:
removeCertClick = (i) => {
let certifications = [...this.state.certifications];
certifications.splice(i, 1);
this.setState({ certifications });
}
我的初始状态:
this.state = {
certifications: [{ certification: '' }]
}
这是我动态呈现 TextInputs 以及删除按钮的代码:
renderCertificationFields = () => {
const { certifications } = this.props.values;
const { handleCertificationChange } = this.props; //receiving as props from parent component
return certifications.map((item, index) => (
<Grid container spacing={1} key={index}>
<Grid item md={10}>
<TextField
label='Certification name'
name='certification'
onChange={(index, event) => handleCertificationChange(index, event)}
defaultValue={item.certification || ''}
/>
</Grid>
<Grid item md={2}>
<Button
onClick={() => removeCertClick(index)}
>Remove</Button>
</Grid>
</Grid>
))
}
感谢您的帮助。
【问题讨论】:
标签: javascript arrays reactjs