簡體   English   中英

console.log(this.props) 未定義

[英]console.log(this.props) undefined

在我的代碼中,當我未定義時,我在 componentDidMount 中使用了 console.log(this.props)。 我在 render() 中的 console.log(this.props) 我得到了四個輸出,前兩個所有屬性都未定義,后兩個是正確的。

componentDidMount = () => {
        const { ratingsAverage } = this.props;
        console.log(ratingsAverage);
        //this.setState({value: this.props.ratingsAverage})
    }

render() {
        console.log(this.props);
 }

componentDidMount() 中 console.log(ratingsAverage) 的 output 未定義。

render() 中 console.log(this.props) 的 output 如下圖所示

在此處輸入圖像描述

嘗試這個。 你的組件構造函數應該是這樣的

constructor(props){
  super(props);
  this.state = {
     //states
  };
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM