TypeError: Невозможно прочитать свойство 'bind' из неопределенного для handleFocus - PullRequest
0 голосов
/ 26 февраля 2019

По какой-то причине я продолжаю получать

TypeError: Невозможно прочитать свойство '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')
);

1 Ответ

0 голосов
/ 26 февраля 2019

Ошибка не с handleFocus, ошибка с handleClick.

вы пытаетесь связать «this» с функцией handleClick, которой не существует.

комментирование строки решает проблему.

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

или вам нужно определить функцию handleClick

...