React native 如何获取平面列表中项目的值?

React native 如何获取平面列表中项目的值?,react-native,redux,React Native,Redux,我试图创建一个项目列表,从中获取值,然后将其存储在redux中,以便稍后在DB中分派。但一切都很顺利,我只是在获取所选项目的价值时遇到了麻烦 下面是我的类别列表视图: <View style={{ flex: 1 }}> <ScrollView style={{ backgroundColor: '#ffffff', }}> <View style={{ flex: 1, flexDirection: 'column', marg

我试图创建一个项目列表,从中获取值,然后将其存储在redux中,以便稍后在DB中分派。但一切都很顺利,我只是在获取所选项目的价值时遇到了麻烦

下面是我的类别列表视图:

<View style={{ flex: 1 }}>
        <ScrollView style={{ backgroundColor: '#ffffff', }}>
          <View style={{ flex: 1, flexDirection: 'column', marginTop: 65, margin: 40 }}>
            <Text style={{ fontSize: 40 }}>Job Category</Text>
            <FlatList
              style={{ marginTop: 20 }}
              data={this.state.jobCategory}
              renderItem={({ item }) => <ListItem data={item} value={item.value} />}
            />
          </View>
        </ScrollView>
        <TouchableHighlight onPress={handleSubmit(_categorySelected)} style={{ backgroundColor: 'white', padding: 20, alignItems: 'center' }} underlayColor="white">
          <Text style={{
            backgroundColor: 'black', color: 'white', fontSize: 20, fontWeight: 'bold',
            height: 50, width: 300, textAlign: 'center', padding: 14
          }}>NEXT</Text>
        </TouchableHighlight>
      </View>

工作类别
}
/>
下一个
这是我的ListItem视图

constructor(props) {
        super(props);
        this.state = {
            selected: false,
            text: props.data.text,
            value: props.data.value
        };

        this.choosen = this.choosen.bind(this);
    }

    choosen(isSelected) {
        this.setState({
            selected: !isSelected,
        });
    }

    render() {
        let backgroundColor = this.state.selected ? "#000000" : "#ffffff";
        let fontColor = this.state.selected ? "#ffffff" : "#000000";
        return (
            <TouchableHighlight selected={this.state.selected} onPress={() => this.choosen(this.state.selected)} underlayColor="black">
                <View style={{backgroundColor: backgroundColor, padding: 20 }}>
                    <Text style={{color: fontColor, fontSize: 20 }}>{this.props.data.text}</Text>
                </View>
            </TouchableHighlight>
        );
    }
构造函数(道具){
超级(道具);
此.state={
选择:false,
text:props.data.text,
值:props.data.value
};
this.choosen=this.choosen.bind(此);
}
乔森(当选){
这是我的国家({
已选定:!已选定,
});
}
render(){
让backgroundColor=this.state.selected?#000000:“#ffffff”;
让fontColor=this.state.selected?#ffffff:“#000000”;
返回(
this.choosen(this.state.selected)}underlayColor=“black”>
{this.props.data.text}
);
}

现在,我想我需要一个标签或我的ListItem的东西,在那里我可以得到所选项目的值。有人有想法吗?

如果我理解正确,您希望类别列表视图存储最后选定列表项的值

最好的办法是向上爬。这就是你要做的:

  • 从列表项组件中删除选定状态,它将作为道具传递给该组件。还可以删除choosen()方法
  • 将selectedItem属性添加到列表组件的状态
  • 向列表组件添加方法以更新selectedItem状态
  • 在FlatList的renderItem道具中,检查当前项目是否等于selectedItem,并将所选道具传递给该检查的结果列表项目
  • 将函数作为prop传递给列表项组件,并将其附加到TouchableHighlight的onPress处理程序。此函数需要链接到列表组件的selectedItem更新程序方法

  • 您希望在什么条件下获得所选项目的值?你想要单选还是多选?@WillCain single select是我需要的