TypeError:无法读取handleFocus的undefined属性'bind'

问题描述 投票:-1回答:1

由于某些原因,我继续得到

TypeError:无法读取undefined的属性'bind',

特别是关于handleFocus(event),我不知道为什么。我检查了我的拼写,似乎是正确的。我不知道问题是什么。这是我的代码,供您参考:

class Main extends React.Component {
  constructor(props){
    super(props);
    this.state = {
      todo: "Type in to-do item",
      todos: []
    };

    this.handleChange = this.handleChange.bind(this);
    this.handleFocus = this.handleFocus.bind(this);
    this.handleClick = this.handleClick.bind(this);
  }

  handleChange(event){
    this.setState({todo: event.target.value});
  }

  handleFocus(event){
    this.setState({todo: ''});
  }

  handleSubmit(event){
    const todos = this.state.todos.slice();
    this.setState({
      todos: todos.concat([
        this.todo
        ]),
      todo: '',
    });
    event.preventDefault();
  }

  render(){
    return (
      <div className = "main">
        <h1>To Do App</h1>
        <form>
          <input type = "text" value = {this.state.todo} 
            onFocus= {this.handleFocus} 
            onChange = {this.handleChange} />
          <input type = "submit" value = "Enter to-do item"
            onClick = {this.handleSubmit}/>
        </form>
        <div className = "tasks">
          <h1>Tasks</h1>
          <button type="button">Clear List</button>
          <button type="button">Reset List</button>
        </div>
      </div>
    );
  }
}

ReactDOM.render(
  <Main />,
  document.getElementById('root')
);
javascript reactjs bind onfocus
1个回答
0
投票

handleFocus没有错误,handleClick错误。

您正试图将“this”绑定到不存在的handleClick函数。

评论该线解决了这个问题。

 // this.handleClick = this.handleClick.bind(this);

或者你需要定义handleClick函数

© www.soinside.com 2019 - 2024. All rights reserved.