使用分段多选反应本机时,undefined不是对象



我有一段代码,它基于https://github.com/renrizzolo/react-native-sectioned-multi-select.

这是我的代码:

import React, { useState } from 'react';
import {I18nManager,View,Text,SafeAreaView,Image,SectionList,TouchableOpacity,TextInput,ScrollView,useWindowDimensions,Linking} from 'react-native';
import Icon from 'react-native-vector-icons/FontAwesome';
import SectionedMultiSelect from 'react-native-sectioned-multi-select';

export function SellWithUsFirstScreen3(props){

const items = [
{  
name: "Fruits",
id: 0,
children: [{
name: "Apple",
id: 10,
},{
name: "Strawberry",
id: 17,
},{
name: "Pineapple",
id: 13,
},{
name: "Banana",
id: 14,
},{
name: "Watermelon",
id: 15,
},{
name: "Kiwi fruit",
id: 16,
}]
},
{
name: "Gems",
id: 1,
children: [{
name: "Quartz",
id: 20,
},{
name: "Zircon",
id: 21,
},{
name: "Sapphire",
id: 22,
},{
name: "Topaz",
id: 23,
}]
},
{
name: "Plants",
id: 2,
children: [{
name: "Mother In Law's Tongue",
id: 30,
},{
name: "Yucca",
id: 31,
},{
name: "Monsteria",
id: 32,
},{
name: "Palm",
id: 33,
}]
},
]

const [selectedItems,setselectedItems] = useState([]);

onSelectedItemsChange = (theitem) => {

setselectedItems(prevState => [...prevState, theitem]);
}
console.log(selectedItems);
/////////
return (
<SafeAreaView style={{flex:1,backgroundColor:'white'}}>
<View style={styles.container}>

<View>
<SectionedMultiSelect
items={items} 
uniqueKey='id'
subKey='children'
selectToggleIconComponent={<Icon name='folder' />}
dropDownToggleIconUpComponent={<Icon name='chevron-up' />}
dropDownToggleIconDownComponent={<Icon name='chevron-down' />}
selectedIconComponent={<Icon name='heart' />}
selectText='Choose some things...'
showDropDowns={true}
readOnlyHeadings={true}
onSelectedItemsChange={this.onSelectedItemsChange}
selectedItems={selectedItems}
IconRenderer={Icon}
/>
</View>

</View>
</SafeAreaView>
);
}

我得到这个错误:

TypeError:undefined不是对象(正在计算"item[subKey]"(。

我尝试了几次代码更改,但仍然无法获得与原始作者视频图像相同的结果。

请帮忙吗?

我认为您的问题是字符串周围需要双引号,因此uniqueKey="儿童";为他们每个人。也没有这个。因此您必须将onSelectedItemsChange={this.onSelectedItemsChange}设置为onSelectedItemsChange={onSelectedItemshange}

最新更新