如何在数组中推送值并将这些值传递给选择标记

时间:2016-07-14 09:52:10

标签: react-native

我想在数组中推送值并将这些值作为select标签的选项传递。我做了以下,

使用插件

import DropDown, {
  Select,
  Option,
  OptionList,
} from 'react-native-selectme';

将状态指定为

this.state = {company:[]};

将此数组推送为

for(let i in data.companyRecord)
    company.push(data.companyRecord[i].companyname);

并指定选择标记为

<Select
        width={250}
        ref="SELECT1"
        optionListRef={this._getOptionList.bind(this)}
        defaultValue="Select a Company ..."
        onSelect={this._company.bind(this)} asyncOptions={this.state.company}>
</Select>

但它不起作用。它显示了

undefined is not an object('evaluating children.length').

请帮我解决这个问题。

1 个答案:

答案 0 :(得分:0)

您无法像这样编辑状态。

const tempNames = [];    
for(let i in data.companyRecord)
    tempNames.push(data.companyRecord[i].companyname);
this.setState({ company: tempNames });

现在你的州将拥有正确的价值。

但是可能仍然存在一些问题,因为您的错误可能表明this.state.company未定义,但是您之前正确地将this.state.company分配给空数组。

这可能是由于this.state未定义。您是否在自己创建的函数中定义了选择器?而不是在你自己的类渲染方法?在这种情况下,您需要将其绑定到您的方法。

    renderSelector() {
        return (<Select
                width={250}
                ref="SELECT1"
                optionListRef={this._getOptionList.bind(this)}
                defaultValue="Select a Company ..."
                onSelect={this._company.bind(this)} asyncOptions={this.state.company}>
        </Select>);
    }

在你的构造函数中,你需要绑定&#34;这个&#34;那个方法。像这样:

this.renderSelector = this.renderSelector.bind(this);