2017-04-24 59 views
-1

我必須在道具上設定價值。如何在道具中設置值以使用其他組件?

class Edit extends Component { 
    onClick() { 
     this.props.userid('5d8d854c8d8dv8'); 
    } 
    render() { 
     return (
     <button className="btn btn-success" onClick={this.onClick.bind(this)}>Add</button> 
    )} 
} 

上面的代碼導致錯誤

this.props.userid不是一個函數

當我安慰this.props我越來越userid: ''。我必須更換道具onClick

+0

閱讀文檔,觀看一些前奏 – webdeb

+0

'userid'是一個字符串,而不是功能。 – elmeister

+0

是的......那麼我如何設置我的值 –

回答

0

您還沒有閱讀文檔正確..

在父組件(假設用戶ID是一個國家),創建一個函數

onUserIDChange(newUserId){ 
     this.setState({ 
     userID: newUserId, 
     }); 
} 

,然後通過這個功能爲道具,以你的編輯零件。

<Edit onUserIDChange = {onUserIDChange} userId={this.state.userID} /> 

那麼你的onClick方法將被

onClick() { 
     this.props.onUserIDChange('5d8d854c8d8dv8'); 
} 
相關問題