2017-09-18 101 views
0

所以我試圖用裁判回調格局這裏解釋:https://facebook.github.io/react/docs/refs-and-the-dom.html,而是繼續得到了一個未定義的家長componentDidMount方法陣營裁判回調格局返回父componentDidMount()未定義

const Child = ({ createFormHeight, getChildRef }) => (
    <CreateForm createFormHeight={createFormHeight}> 
    // getting reference from here 
    <div ref={getChildRef}> 
     // form markup here 
    </div> 
    </CreateForm> 
); 

class Parent extends Component { 
    constructor(props) { 
    super(props); 
    this.state = { 
     createFormHeight: '', 
    } 
    } 

    componentDidMount() { 
    const createFormHeight = this.formWrapper.offsetHeight; // returns 
error that this.formWrapper is undefined 
    this.setFormHeight(createFormHeight); 
    } 

    getChildRef = (el) => { 
    // if I console.log(el) it returns the div here 
    this.formWrapper = el; 
    } 

    setFormHeight = (height) => { 
    this.setState(() => ({ createFormHeight: height })); 
    }; 

    render() { 
    const { createIsOpen, createFormHeight } = this.state; 
    return (
     <CreateMember createFormHeight={createFormHeight} getChildRef= 
{this.getChildRef} /> 
    ); 
    } 
} 
+0

將一個'console.log'添加到'componentDidMount',另一個添加到'getChildRef',看哪個先登錄。 – bennygenel

回答

0

我想這個問題你沒有把你的getChildRef上傳到上下文中。嘗試將this.getChildRef = tihs.getChildRef.bind(this)添加到構造函數