我有一個組件A
:陣營獲得的子組件道具內部函數使用
import React, { Component } from 'react'
import Gmap from '../global/gmap.component'
class RandomPlace extends Component {
render() {
return (
<Gmap address={this.state.random.location} />
除其他事項外,其呈現的Gmap
組件:
class Gmap extends Component {
componentDidMount() {
}
render() {
return (
<div className="gmap-component">
<p>{this.props.address}</p>
這<p>{this.props.address}</p>
很好地顯示和更新當我點擊組件A
上的「重新加載」按鈕時。此時,React Chrome擴展程序很好地顯示了道具的地址內容。並且看到它在「重新加載」操作中得到更新。
問題是,我不能似乎能夠在我的組件Gmap
的內部功能到達道具屬性address
像componentDidMount()
或aCustomFunction()
。在類的頂部
componentDidMount() {
console.log('gmap did mount')
this.setState({address: this.props.address})
let x = this.props.address
let y = this.state.address
console.log(x)
console.log(y)
使用構造:
我已經測試了這個
constructor (props) {
super(props)
this.state = {
address: 'xx'
}
但什麼也不顯示。我是React新手,肯定我缺少一些非常基本的東西,但無法找到它。 在此先感謝。
你在日誌中看到什麼? – Andrew
我得到了'undefined'和'xx'。 – kevin
最好在你返回你的jsx之前檢查道具和狀態渲染,同時也應對chrome開發工具在調試這種情況時的幫助。 – Mateusz