【问题标题】:Unexpected usage of event in create-react-app在 create-react-app 中意外使用事件
【发布时间】:2019-03-27 17:52:44
【问题描述】:

我想用 create-react-app 编写简单的表单,但我不能,因为我不能使用事件对象。我得到的错误是 ./src/components/Form.js 第 14 行:意外使用 'event' no-restricted-globals

这是最新的 create-react-app 的问题。代码来自 react docs。

import React from 'react';

class Form extends React.Component {
  constructor(props) {
    super(props);
    this.state = {value: ''};
  }

  handleChange = e => {
    this.setState({value: e.target.value});
  }

  handleSubmit = e => {
    event.preventDefault();
  }

  render() {
    return (
      <form onSubmit={this.handleSubmit}>
        <label>
          Name:
          <input type="text" value={this.state.value} onChange={this.handleChange} />
        </label>
        <input type="submit" value="Submit" />
      </form>
    );
  }
}

export default Form;

【问题讨论】:

  • 第 14 行 e.preventDefault()

标签: reactjs eslint create-react-app


【解决方案1】:

你的handleSubmit函数应该改变如下

handleSubmit = e => {
    e.preventDefault();
  }

【讨论】:

  • 我在使用 vim 时遇到了问题。 Vim 不想保存。更改后我现在有错误 Uncaught TypeError: Super expression must be null or a function
  • @KamilStaszewski 你能告诉我们你的新错误是什么样子的吗?我的回答也解决了“意外使用'事件'无限制全局”错误吗?
猜你喜欢
  • 1970-01-01
  • 2019-09-11
  • 1970-01-01
  • 2022-11-21
  • 2023-01-26
  • 2019-04-09
  • 2021-08-11
  • 2017-10-15
  • 2019-08-24
相关资源
最近更新 更多