【问题标题】:onChange In SelectField Component For Material-UI / React is not accepting onChange from parent ComponentMaterial-UI/React 的 SelectField 组件中的 onChange 不接受来自父组件的 onChange
【发布时间】:2017-08-15 19:27:19
【问题描述】:

所以我的工作场所在多个地方使用的组件内部使用了 SelectField。说得通。现在,当我在我正在处理的当前组件中使用该组件时。我创建的 onChange 事件没有传递正确完成所需的值。现在,如果我将 SelectField 更改为 onChange={onChange} 它工作正常。但是,它不能这样设置,因为它在应用程序中的其他部分都依赖于它。我是 React 的新手。这是我的功能

  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

  <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 本身

 <SelectField
   name={name}
   maxHeight={300}
   value={value}
   onChange={(event, index, response) => onChange(response)}
   disabled={disabled}
   hintText={hint}
  >

提前谢谢你。

【问题讨论】:

  • 如果我理解正确,那么像这样更改 SelectField 的 onChange 道具应该可以解决您的问题 => onChange={(event, index, response) => onChange(event, index, response)}
  • 完美,谢谢

标签: javascript reactjs material-ui


【解决方案1】:

您从 addHistory 返回的方法有 4 个参数,即 event, index, value, target 而您从 SelectField 组件调用的方法,即 onChange={(event, index, response) =&gt; onChange(response)} 它只有一个参数,而且响应也与第一个参数不匹配上面提到的返回方法。

【讨论】:

  • 谢谢你,这很有道理,我觉得很愚蠢,因为我不明白这一点。谢谢你的回答。
  • 没有问题...我很高兴
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-01-18
  • 1970-01-01
  • 1970-01-01
  • 2019-06-12
  • 1970-01-01
  • 2017-11-15
  • 1970-01-01
相关资源
最近更新 更多