【问题标题】:Material-ui Button onClick() causes "Uncaught TypeError: Cannot read property 'name' of undefined" errorMaterial-ui Button onClick() 导致“Uncaught TypeError: Cannot read property 'name' of undefined”错误
【发布时间】:2018-06-22 04:04:52
【问题描述】:

我有一个具有 OnClick 功能的材质 ui 按钮。但是由于某种原因,当我单击该按钮时,它不起作用并给了我上面显示的错误。

我可以知道我在这里做错了什么吗? 我使用了这里建议的构造函数:suggested solution 但是还是不行

道具

type Props = {
  showSmallHero: () => void,
  fetchTalentIfNeeded: () => void,
  submitJobApplicationIfNeeded: () => void,
  talent: Object,
};

我的代码

  componentDidMount() {
    this.props.showSmallHero();
    this.props.fetchTalentIfNeeded();
  }

  handleChange = (event, d) => {
    const { target } = event;
    const value = target.type === 'checkbox' ? target.checked : target.value;
    if (d.name === 'gender') {
      this.setState({
        [d.name]: d.value,
      });
    } else if (d.name === 'resume') {
      // This is a file
    } else {
      const { name } = target;
      this.setState({
        [name]: value,
      });
    }
  };

  handleSubmit = () => {
    const fileInput = document.getElementById('resume');
    const file = fileInput.files[0];
    const formData = new FormData();
    formData.append('full-name', this.state.fullName);
    formData.append('gender', this.state.gender);
    formData.append('nric', this.state.nationalId);
    formData.append('email', this.state.email);
    formData.append('phone-number', this.state.phoneNumber);
    formData.append('position-applied', this.state.jobName);
    formData.append('job-id', this.state.jobId);
    formData.append('experience', this.state.experience);
    formData.append('academic-result', this.state.academicResult);
    formData.append('resume', file);
    this.props.submitJobApplicationIfNeeded(formData);
  };

  handleClick = (e, d) => {
    this.setState({ jobName: d.name, jobId: d.id, showModal: true });
  };

  hideModal = () => {
    this.setState({ showModal: false });
  };


render() {

<Button
     variant="contained"
     size="medium"
     color="secondary"
     id={t.acf.job_id}
     name={t.acf.job_name}
     onClick={this.handleClick}
     >
     Apply
     </Button>
}

非常感谢任何帮助。

【问题讨论】:

  • 如果我错了,请纠正我,因为我可以看到方法定义中有两个参数,并且点击时只有事件对象!
  • 那个错误是因为那个d对象或参数未定义或为空

标签: javascript reactjs button web material-ui


【解决方案1】:

您没有在 onClick 方法上使用 handleClick 函数传递任何参数,并且您试图从未定义的参数中访问某些键,这就是您收到错误 cannot read property name of undefined 的原因。尝试将参数传递给该 handleClick 函数:

<Button
 variant="contained"
 size="medium"
 color="secondary"
 id={t.acf.job_id}
 name={t.acf.job_name}
 onClick={()=>this.handleClick(param1,param2)}
 >
 Apply
 </Button>

【讨论】:

  • 我使用wordpress无头api作为后端,e&d参数是wordpress处理的。当我使用语义用户界面按钮时,该按钮有效。在我将按钮更改为material-ui后,OnClick不起作用。
  • 但是您提到的错误是由于参数引起的。如果您认为您的按钮 onclick 不起作用,请尝试将 console.log('working') 放入您的 handleClick 中。我不认为你的按钮有问题
  • 刚试过console.log,是的你是对的,错误是参数没有传入参数...不知道为什么会这样。
  • 那是因为你得到的参数是 undefined 。在你的函数中尝试console log('here',d),你会更清楚
  • 将最少的代码添加到沙箱中,以便我可以在此问题上为您提供更多帮助
猜你喜欢
  • 1970-01-01
  • 2017-04-24
  • 1970-01-01
  • 2023-03-15
  • 1970-01-01
  • 2013-01-15
  • 1970-01-01
  • 2019-02-12
  • 2021-09-20
相关资源
最近更新 更多