简体   繁体   English

如何修复:“类型错误:无法读取 null 的属性‘打印类型’”

[英]How to fix: "TypeError: Cannot read property 'print-type' of null"

I'm building a google book search app, and before I even start I open up the inspect and I see on my console我正在构建一个谷歌图书搜索应用程序,在我开始之前,我打开了检查并在我的控制台上看到

"Warning: Can't call setState on a component that is not yet mounted". “警告:无法在尚未安装的组件上调用 setState”。

Also, just to see what other errors that come up I did a book search and the error that I am getting back says另外,只是为了查看出现的其他错误,我进行了书籍搜索,而我返回的错误说

"TypeError: Cannot read property 'print-type' of null". “类型错误:无法读取 null 的属性‘打印类型’”。 And the console is pointing to this line of code as the problem "this.state["print-type"]"并且控制台指向这行代码作为问题“this.state[”print-type”]”

I'm new to react so right now I am all thumbs do I need to put a componentDidMount within my handler or will I need to further bind this handler in order for it to work?我是新来的反应所以现在我全都大拇指我需要在我的处理程序中放置一个 componentDidMount 还是我需要进一步绑定这个处理程序以使其工作?

In my constructor I have inserted this.handleSearch = this.handleSearch.bind(this) thinking that this will solve my problem with在我的构造函数中,我插入了this.handleSearch = this.handleSearch.bind(this)认为这将解决我的问题

"Can't call setState on a component that is not yet mounted." “无法在尚未安装的组件上调用 setState。”

but this did not work.但这不起作用。

constructor(props) {
    super(props);
    this.setState({
      "print-type": "all",
      filter: "ebooks"
    });
    this.handleSearch = this.handleSearch.bind(this);
  }

  changeFilter(e) {
    const filter = e.target.value();
    this.setState({
      filter
    });
  }

  changePrintType(e) {
    const printType = e.target.value();
    this.setState({
      "print-type": printType
    });
  }

  handleSearch(q) {
    console.log(this.state);
    const key = "AIzaSyAJ448LHnJ0N6XxzOyIRfhJFveQzwHU_Ms";
    const baseurl = "https://www.googleapis.com/books/v1/volumes";
    const url = `${baseurl}?q=${encodeURIComponent(q)}&key=${key}&print-type=${
      this.state["print-type"]
    }&filter=${this.state.filter}`;
    const options = {
      headers: {
        "Content-Type": "application/json"
      }
    };

I expect to see all books with the same title I search for.我希望看到我搜索的所有同名书籍。

The correct way of initialising the state is - 初始化状态的正确方法是-

this.state = {
      print-type: "all",
      filter: "ebooks"
    };

The correct way of setting the state is - 设置状态的正确方法是-

this.setState({
      print-type: printType
    });

"Warning: Can't call setState on a component that is not yet mounted" is the result of setState() inside constructor() . "Warning: Can't call setState on a component that is not yet mounted"constructor()setState()的结果。 You need to change it to this.state because you're just defining your state there. 您需要将其更改为this.state因为您只是在此处定义状态。

We never write setState in constructor, 我们永远不会在构造函数中编写setState

constructor(props) {
    super(props);
    this.setState({
      "print-type": "all",
      filter: "ebooks"
    });
    this.handleSearch = this.handleSearch.bind(this);
  }

You need to define state, 您需要定义状态,

constructor(props) {
    super(props);
    this.state ={
      print_type: "all",   //never write state name like `print-type`, use underscore instead
      filter: "ebooks"
    }
    this.handleSearch = this.handleSearch.bind(this);
  }

Whenever you want to use print_type write like , this.state.print_type . 每当您要使用print_typethis.state.print_type编写this.state.print_type

For setState, 对于setState,

this.setState({print_type:printType})

To answer your main question, calling setState inside the constructor is a big no no in React because you have not yet defined the component's initial state . 要回答您的主要问题,在React中调用setState是一个很大的否定,因为您尚未定义组件的初始状态 You are trying to alter something that isn't defined. 您正在尝试更改未定义的内容。

Instead of this.setState({...}) in your constructor, use this.state = {...} to define the initial state of your component. 代替构造函数中的this.setState({...}) ,使用this.state = {...}定义组件的初始状态。 You also have a lot of other issues in your code besides that. 除此之外,您的代码中还有很多其他问题。 However, your main issue is the constructor of your component. 但是,您的主要问题是组件的构造函数。 That is called before it is 'mounted.' 在“挂载”之前调用它。 Once a component is 'mounted,' you may then alter the component's state by using this.setState({...}) . 一旦“挂载”了组件,您就可以使用this.setState({...})来更改组件的状态。

Other things I can see that are either incorrect or not good practice are: 我看到的其他错误或不正确的做法是:

  • You are using hyphens in JSON. 您正在JSON中使用连字符。 Don't use hyphens. 不要使用连字符。 Use traditional camel-casing, ie printType . 使用传统的骆驼壳,即printType

  • Your change filter function is also incomplete, but you likely know that. 您的变更筛选器功能也不完整,但是您可能知道这一点。

  • e.target.value() is not valid because e.target.value is not a function. e.target.value()无效,因为e.target.value不是函数。 Use e.target.value from here on out. 从此开始使用e.target.value

  • Lastly, you also need to bind your changeFilter and changePrintType functions the same way you are binding your handleSearch function. 最后,您还需要你的绑定changeFilterchangePrintType功能,您有约束力的相同的方式handleSearch功能。

In my honest and humble opinion, you should probably take just a quick 20 minute JavaScript refresher. 以我的诚实和谦逊的观点,您可能应该只花20分钟快速浏览一下JavaScript。 I feel like trying to learn a library without mastering the basics is going to make learning the React library more difficult than it needs to be. 我觉得试图在不掌握基础知识的情况下学习一个库将使学习React库比需要的更加困难。

暂无
暂无

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

相关问题 如何在React中的Array中修复TypeError“无法读取null的属性'x'” - How to fix TypeError “cannot read property 'x' of null” at Array in React 如何解决“未捕获的TypeError:无法读取null的属性'click'” - How to fix “Uncaught TypeError: Cannot read property 'click' of null” 如何解决:“未捕获的TypeError:无法读取null的属性'addEventListener'”…? - how to fix it: “Uncaught TypeError: Cannot read property 'addEventListener' of null”…? 如何修复:“类型错误:无法读取 null 的属性 'addEventListener'”...?// - how to fix : " TypeError: Cannot read property 'addEventListener' of null”…?// 如何修复此类型错误:无法读取 null 的属性“名称” - how to fix this TypeError: Cannot read property 'name' of null 如何修复未捕获的类型错误:无法读取 null 的属性“孩子” - How to fix Uncaught TypeError: Cannot read property 'children' of null 如何解决“ Uncaught TypeError:无法读取null的属性'value'”错误? - How to Fix “Uncaught TypeError: Cannot read property 'value' of null” error? 如何修复未捕获的 TypeError:无法读取 null 的属性“outerHTML” - How to fix uncaught TypeError: Cannot read property 'outerHTML' of null 如何解决:TypeError:无法在我的代码中读取null的属性“ join”? - How to fix: TypeError: Cannot read property 'join' of null on my code? 如何修复未捕获的类型错误:无法读取 null 的属性“值” - how to fix the Uncaught TypeError: Cannot read property 'Value' of null
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM