简体   繁体   English

为什么我收到无法读取未定义的属性“状态”?

[英]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.

相关问题 为什么我收到 TypeError:无法读取未定义的属性“执行” - Why am I receiving TypeError: cannot read property 'execute' of undefined 为什么在定义的数组上收到“ ERROR TypeError:无法读取未定义的属性&#39;length&#39;的信息”? - Why am I receiving “ERROR TypeError: Cannot read property 'length' of undefined” on a defined array? 无法读取推送未定义的属性? 即使我将评论推送到评论数组,为什么我会收到此错误? - Cannot read property of push undefined? Even though I am pushing comment to comments array why am I receiving this error? 为什么我在 vue js 中收到“无法读取未定义的属性‘状态’”的错误 - why i am getting error of "cannot read property 'state' of undefined " in vue js 我为什么会收到TypeError:无法读取未定义的属性“现在” - Why am I getting TypeError: Cannot read property 'now' of undefined 为什么我在此 for 循环中无法读取未定义的属性“startsWith”? - Why am I getting Cannot read property 'startsWith' of undefined in this for loop? 为什么会出现“无法读取未定义的html属性”? - Why am I getting “Cannot read property of html undefined”? 为什么我在这个验证器上有“无法读取未定义的属性‘http’”? - Why am i having “Cannot read property 'http' of undefined” on this validator? 为什么我越来越无法读取未定义错误的属性? - why I am getting, cannot read property of undefined error? 为什么在此示例中出现“无法读取未定义的属性&#39;文本&#39;”? - Why am I getting “Cannot read property 'text' of undefined” in this example?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM