[英]Why am I receiving Cannot read property 'state' of undefined?
I have a constructor that suddenly stopped working, can't figure out why. 我有一个突然停止工作的构造函数,不知道为什么。
I have this data. 我有这个数据。 If you see at line 24, this is read out and actually returns various items including props.
如果您在第24行看到此内容,它会被读出并实际上返回包括道具在内的各种物品。 However 1 line later when I try to write to state, it returns this error.
但是,当我尝试写入状态后的1行,它将返回此错误。 I can't figure out why as there's nothing to bind here and it was working at some point.
我不知道为什么,因为这里没有任何可绑定的东西,并且它在某个时候还在工作。
TypeError: Cannot read property 'state' of undefined 23 | super(props); 24 | console.log(this) 25 | 26 | this.state = { | ^ 27 | loading: true, 28 | ...props.location.state, 29 | review: props.review,
import React, { Component } from 'react';
import { withFirebase } from '../Firebase';
import { AuthUserContext } from '../Session';
import { needsEmailVerification } from '../Session/withEmailVerification.js'
import NewCommentReactionPage from '../Comments/newReaction.js'
import NewReviewFeedbackPage from '../Comments/new.js'
import RequireEmailComponent from '../Errors/requireEmail.js'
const ReviewFeedbackPage = (props) => (
<AuthUserContext.Consumer>
{authUser => (
<ReviewFeedbackMain authUser={authUser} isLoggedIn={condition(authUser)} needEmail={needsEmailVerification(authUser)} {...props} />
)}
</AuthUserContext.Consumer>
);
class ReviewFeedbackBase extends Component {
constructor(props) {
super(props);
this.state = {
loading: true,
...props.location.state,
review: props.review,
newComment: '',
comments: [],
reactions: props.review ? props.review.count ? props.review.count.reactions ? props.review.count.reactions : {} : {} : {}
};
this.reactionAdded = this.reactionAdded.bind(this)
this.commentAdded = this.commentAdded.bind(this)
}
componentDidMount() {
...
}
componentDidUpdate(prevProps, prevState) {
...
}
commentAdded(newComment){
...
}
render() {
const { review, comments, reactions } = this.state;
const { isLoggedIn, needsEmail } = this.props;
return (
<div>
....
}
{isLoggedIn && needsEmail && <RequireEmailComponent toAction={{text: 'to react'}} />}
{isLoggedIn && !needsEmail &&
<div>
<NewCommentReactionPage review={review} reactionAdded={this.reactionAdded} />
<NewReviewFeedbackPage review={review} commentAdded={this.commentAdded} />
</div>
}
</div>
);
}
}
const ReviewFeedbackMain = withFirebase(ReviewFeedbackBase);
const condition = authUser => !!authUser;
export default ReviewFeedbackPage;
In your code, you have 2 state
: 在您的代码中,您有2个
state
:
this.state = { // declaring state
loading: true,
...props.location.state, // referencing the state of ...props.location
review: props.review,
newComment: '',
comments: [],
reactions: props.review ? props.review.count ? props.review.count.reactions ? props.review.count.reactions : {} : {} : {}
};
You are getting this error because props.location
is undefined
and you are trying to access state
from it. 您收到此错误是因为
props.location
undefined
并且您试图从中访问state
。
The error is in line 26 because it's not being able to declare the variable. 错误在第26行,因为它无法声明变量。
26 | this.state = { | ^ 27 | loading: true,
Not a problem in the constructor, but in the ...props.location.state
. 在构造函数中不是问题,但是在
...props.location.state
。
Now that you know what is the problem, you need to see what you want to do. 现在您知道了问题所在,您需要查看要执行的操作。
Maybe do some checking before spreading props.location.state
? 也许在传播
props.location.state
之前进行一些检查? It's up to you. 由你决定。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.