2016-09-15 52 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

相关问题