[英]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_type
, this.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. 最后,您还需要你的绑定
changeFilter
和changePrintType
功能,您有约束力的相同的方式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.