React App-尝试控制动态创建的材质用户界面TextInput,但不起作用

时间:2020-05-13 08:16:55

标签: javascript arrays reactjs

每当我在输入字段中键入任何内容时,我都会收到“ TypeError:无法读取未定义的属性'target'”。这是错误在父组件中指向我的代码:

handleCertificationChange = (index, e) => {
   let certifications = [...this.state.certifications];
   certifications[index] = {...certifications[index], [e.target.name]: e.target.value};
   this.setState({ certifications });
}

我的初始状态:

this.state = {
   certifications: [{ certification: '' }]
}

这是我在子组件中调用TextField的方式:

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>
        ))
    }

任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:1)

在JSX中,document.wrappedJSObject.monetization对象是event回调中的第一个也是唯一的参数-因此,您无需在回调中定义onChange,仅需要自己定义方法。

index