所以我的工作場所在一個組件內使用SelectField,這個組件在多個地方使用。說得通。現在,當我在當前正在使用的那個組件中使用該組件時。我創建的onChange事件不會傳遞正確完成所需的值。現在,如果我將SelectField更改爲onChange = {onChange},它可以正常工作。但是,它不能以這種方式設置,因爲它內部的其他部分依賴於它。我是React新手。這裏是我的功能onChange在SelectField組件中Material-UI/React不接受來自父組件的onChange
addHistory = (field) => (event, index, value, target) => {
const { history } = this.state;
console.log(this.state.history);
console.log(field);
if (field === 'employerName' || field === 'description') {
history[field] = event.target.value;
} else if (field === 'roles') {
history[field] = value;
} else {
history[field] = event.value;
}
this.setState({
history: this.state.history,
});
}
這裏是我的導入和與組件
<CompanySelectField
label={translations.translate('driverProperties', 'typeOfVehicle')}
hint={translations.translate('driverProfile', 'pleaseSelectRole')}
style={{ marginTop: '25px' }}
name={'roles'}
value={this.state.history.roles}
onChange={this.addHistory('roles')}
fullWidth
required
>
這裏使用CompanySelectField是CompanySelectField本身
<SelectField
name={name}
maxHeight={300}
value={value}
onChange={(event, index, response) => onChange(response)}
disabled={disabled}
hintText={hint}
>
謝謝你的時間提前。
如果我正確地理解了你,那麼改變SelectField的onChange這個prop應該可以解決你的問題=> onChange = {(event,index,response)=> onChange(event,index,response)} –
完美,謝謝 –