简体   繁体   English

为什么我不断收到此错误? 未捕获的类型错误:无法读取 null 的属性(读取“术语”)

[英]why do i keep getting this error? Uncaught TypeError: Cannot read properties of null (reading 'term')

this is my code but I keep getting this error这是我的代码,但我不断收到此错误

Uncaught TypeError: Cannot read properties of null (reading 'term')未捕获的类型错误:无法读取 null 的属性(读取“术语”)

import React from "react";


class SearchBar extends React. Component{
   State = { term: ''};

  onInputChange = event => {
     this.setState({ term: event.target.value});

 };
  onFormSubmit = event => {
    event.preventDefault();
      this.props.onSubmit(this.state.term)
  }

    render(){
        return(
            <div className="Search-bar ui segment ">
                <form  onSubmit={this.onFormSubmit} className="ui form">
                    <div className="field">
                        <label>Video Search</label>
                        <input className="ui input"
                         type="text" 
                         value={this.state.term}
                         onChange={this.onInputChange}
                         />
                    </div>
                </form>
            </div>
        );
    }
}

export default SearchBar;

The proper way to use state in class component is to declare state in the constructor, and use this.setState to modify state.在 class 组件中使用 state 的正确方法是在构造函数中声明 state,并使用this.setState修改 Z9ED36A9E2EA9934258。

Also, you need to declare functions not as a variable, but a class instance method and bind them inside the constructor.此外,您需要将函数声明为不是变量,而是 class 实例方法并将它们绑定在构造函数中。

The code would be:代码将是:

import React from 'react';

class SearchBar extends React.component {
  constructor(props) {
    super(props);
    this.state = {
      term: ''
    };
    this.onInputChange = this.onInputChange.bind(this);
    this.onFormSubmit = this.onFormSubmit.bind(this);
  }
  onInputChange(event) {
    this.setState({ term: event.target.value });
  }
  onFormSubmit(event) {
    event.preventDefault();
    this.props.onSubmit(this.state.term);
  }
  render() {
    return (
      <div className="Search-bar ui segment ">
        <form  onSubmit={this.onFormSubmit} className="ui form">
          <div className="field">
            <label>Video Search</label>
            <input className="ui input"
              type="text" 
              value={this.state.term}
              onChange={this.onInputChange}
            />
          </div>
        </form>
      </div>
    );
  }
}
export default SearchBar;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我不断收到此错误? 未捕获的类型错误:无法读取 null 的属性“classList” - Why do I keep getting this error? Uncaught TypeError: Cannot read property 'classList' of null 我只是想验证这个表单,但我不断收到 Uncaught TypeError: Cannot read properties of null (reading &#39;style&#39;) - i just want to validate this form but i keep getting Uncaught TypeError: Cannot read properties of null (reading 'style') 代码运行正常,但为什么我在控制台出现错误,Uncaught TypeError: Cannot read properties of null (reading 'style')? - Code is working and runs fine, but why I'm getting error at console , Uncaught TypeError: Cannot read properties of null (reading 'style')? 未捕获的类型错误:无法读取 null 的属性(读取“addEventListener”)。 该怎么办? - Uncaught TypeError: Cannot read properties of null (reading 'addEventListener'). What to do? 为什么我有一个错误“TypeError: Cannot read properties of null (reading &#39;getTracks&#39;)”,但代码可以工作? - Why do I have an error "TypeError: Cannot read properties of null (reading 'getTracks')", but the code is working? 为什么这段代码给了我“未捕获的类型错误:无法读取 null 的属性(读取 &#39;push&#39;)”错误? - Why this code gives me "Uncaught TypeError: Cannot read properties of null (reading 'push')" error? 我在基本 JS 按钮中收到“Uncaught TypeError: Cannot read properties of null (reading 'addEventListener')”错误 - I get the "Uncaught TypeError: Cannot read properties of null (reading 'addEventListener')" error in a basic JS button 未捕获的类型错误:无法读取 null 的属性(正在读取“切片”)------ 未捕获的类型错误:无法读取未定义的属性(正在读取“过滤器”) - Uncaught TypeError: Cannot read properties of null (reading 'slice') ------ Uncaught TypeError: Cannot read properties of undefined (reading 'filter') 出现错误:TypeError:无法读取 null 的属性(读取“0”) - Getting Error: TypeError: Cannot read properties of null (reading '0') 我仅在Chrome中不断收到以下错误:未捕获的TypeError:无法读取null的属性“值” - I keep getting the following error in Chrome only: Uncaught TypeError: Cannot read property 'value' of null
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM