2016-09-15 51 views
0

在reactjs中,我們將更改節點委託給渲染方法,一旦完成修改,我們如何調用或獲取像selectedIndex這樣的屬性?我選擇selectedIndex,因爲即使用戶沒有真正選擇任何東西,它也是0。如何在render()中調用.selectedIndex?

例如:

render() { 
    <CategoryInput 
     categories={this.state.categories} 
     onValueChange={function(event) { 
     // not called when options themselves have changed 
     }} 
     ref={(input) => { 
     if (input) { 
      // the is a reference to the instance 
     } 
     }} 
    /> 

    // now that the categories have been added to the component 
    // I'd like to retrieve selectedIndex to use on a sibling's component 
} 

回答

0
任何

部件是這樣做的render方法需要有selectedIndex作爲其狀態的一部分。所以,你可以有一個

getInitialState: function() { 
return this.setState({ selectedIndex: this.state.category[0] }) 
} 

設置初始狀態時,它呈現並顯示所選類別 然後onValueChange功能也應的setState到任何類別又改爲

+0

我擔心的是我沒有從節點本身獲取selectedIndex,而是我假設它會是什麼。這是保證在規範?在reactjs之外,我只使用本地DOM過程。 – chrisp

+0

https://facebook.github.io/react/docs/forms.html#why-select-value這將通過處理選擇的React方式。它使用選擇的值來確定一切。您不必擔心selectedIndex,只需確保該值與您在下拉菜單中輸入的值相匹配 – finalfreq

+1

謝謝。這已經足夠了。 (感謝鏈接到文章我必須讀其他原因一百萬次 - 錯過了選擇部分) – chrisp

相關問題