Javascript 如何从react native中的Picker onValueChange获取标签

Javascript 如何从react native中的Picker onValueChange获取标签,javascript,react-native,Javascript,React Native,我想获取项目的值(在RNPickerSelect中)。 ex1)如果我的项目是uc,那么我希望得到标签(“ucCollege”)如下.state.text。 ex2)如果this.state.main\u cate是“portal”,那么我想获得const numl的标签。 最后,我想像最后一行代码一样将标签打印为文本。 我该怎么做 const uc = [ { label: "ucCollege", value: "uc", },

我想获取项目的值(在RNPickerSelect中)。 ex1)如果我的项目是
uc
,那么我希望得到
标签(“ucCollege”)如下.state.text
。 ex2)如果
this.state.main\u cate是“portal”
,那么我想获得const numl的标签。 最后,我想像最后一行代码一样将标签打印为文本。 我该怎么做

const uc = [
  {
    label: "ucCollege",
    value: "uc",
  },
];
const isa = [
  {
    label: "isaCollege",
    value: "isa",
  },
];
const nul = [
  {
    label: "nulCollege",
    value: "nul",
  },
];
export default class URL extends Component {
  constructor(props) {
    super(props);
    this.state = {
      main_cate: undefined,
      sub_cate: undefined,
      text: undefined,
    };
  }
  render() {

    return (
      <View>
        ...

        <RNPickerSelect
          placeholder={sub_placeholder}
          items={
            this.state.main_cate === "portal"
              ? nul
              : this.state.main_cate === "uc"
              ? uc
              : isa  (Actually there is more, but i comfortably omit that)
          }
          onValueChange={(value, index) => {
            this.setState({
              sub_cate: value,
              text: items[index].label,          // this is the point !!!
            });
          }}
          value={this.state.sub_cate}
          //label={this.state.sub_cate}
        />
        <Text>{this.state.text}</Text>          // And I want to print text like this
      </View>
    );
  }
}
const uc=[
{
标签:“加州大学学院”,
值:“uc”,
},
];
常数isa=[
{
标签:“伊萨学院”,
值:“isa”,
},
];
常数nul=[
{
标签:“国立大学”,
值:“nul”,
},
];
导出默认类URL扩展组件{
建造师(道具){
超级(道具);
此.state={
主要类别:未定义,
子类别:未定义,
文本:未定义,
};
}
render(){
返回(
...
{
这是我的国家({
子类别:价值,
text:items[index].label,//这就是重点!!!
});
}}
值={this.state.sub_cate}
//label={this.state.sub_cate}
/>
{this.state.text}//我想这样打印文本
);
}
}

您可以像这样直接引用uc数组,而不是项目,索引将选择标签

 onValueChange={(value, index) => {
        this.setState({
          sub_cate: value,
          text: uc[index].label, // this is the point !!!
        });
      }}
完整代码:

  const uc = [
      {
        label: "ucCollege",
        value: "uc",
      },
    ];
    const isa = [
      {
        label: "isaCollege",
        value: "isa",
      },
    ];
    const nul = [
      {
        label: "nulCollege",
        value: "nul",
      },
    ];
    export default class URL extends Component {
      constructor(props) {
        super(props);
        this.state = {
          main_cate: undefined,
          sub_cate: undefined,
          text: undefined,
        };
      }
      render() {
        return (
          <View>
            ...
            <RNPickerSelect
              placeholder={sub_placeholder}
              items={
                this.state.main_cate === "portal"
                  ? nul
                  : this.state.main_cate === "uc"
                  ? uc
                  : isa //(Actually there is more, but i comfortably omit that)
              }
              onValueChange={(value, index) => {
                this.setState({
                  sub_cate: value,
                  // instead of items you can directly refference your uc array like this
                  text: uc[index].label, // this is the point !!!
                });
              }}
              value={this.state.sub_cate}
              //label={this.state.sub_cate}
            />
            <Text>{this.state.text}</Text> // And I want to print text like this
          </View>
        );
      }
const uc=[
{
标签:“加州大学学院”,
值:“uc”,
},
];
常数isa=[
{
标签:“伊萨学院”,
值:“isa”,
},
];
常数nul=[
{
标签:“国立大学”,
值:“nul”,
},
];
导出默认类URL扩展组件{
建造师(道具){
超级(道具);
此.state={
主要类别:未定义,
子类别:未定义,
文本:未定义,
};
}
render(){
返回(
...
{
这是我的国家({
子类别:价值,
//您可以像这样直接引用uc数组而不是项
text:uc[index].label,//这就是重点!!!
});
}}
值={this.state.sub_cate}
//label={this.state.sub_cate}
/>
{this.state.text}//我想这样打印文本
);
}
我希望这能帮到你