在 React Native 中如何使用 this.state 访问数组中的对象?
How do you access an object in an array with this.state in React Native?
我正在使用 react-native-snap-carousel,但无法访问数组中的 URL。
我可以在没有其他对象的情况下访问它们,但我需要它们来命名每个图像。
constructor(props)
{
super(props);
this.state = {
categoriesimages: [
{
name: 'business 1',
images:
'http://www.example.com/img/venture.jpg',
},
{
name: 'business 2',
images:
'http://www.example.com/img/venture.jpg',
},
{
name: 'business 3',
images:
'http://www.example.com/img/venture.jpg',
}
]
}
renderItem = ({ item }) => {
return (
<Image source={{ uri: item }} style={styles.logoStyle} />
);
}
<View>
<Carousel
inactiveSlideOpacity={0.6}
inactiveSlideScale={0.65}
firstItem={1}
sliderWidth={width}
itemWidth={width / 3}
data={this.state.categoriesimages['images']} // this is the problem
renderItem={this.renderItem}
containerCustomStyle={{ overflow: 'visible' }}
contentContainerCustomStyle={{ overflow: 'visible' }}
/>
</View>
我还是 React Native 的新手,查找类似的问题并没有找到正确的答案。
您可以为此使用地图 this.state.categoriesimage.map(value => value.image)
此处this.state.categoriesimages['images']
,data参数接受array
格式。
现在您正在尝试访问索引为 images
的 categoriesimages
因此您需要将其替换为
data={this.state.categoriesimages}
和
renderItem={({item}) => console.log(item.images)}
我正在使用 react-native-snap-carousel,但无法访问数组中的 URL。 我可以在没有其他对象的情况下访问它们,但我需要它们来命名每个图像。
constructor(props)
{
super(props);
this.state = {
categoriesimages: [
{
name: 'business 1',
images:
'http://www.example.com/img/venture.jpg',
},
{
name: 'business 2',
images:
'http://www.example.com/img/venture.jpg',
},
{
name: 'business 3',
images:
'http://www.example.com/img/venture.jpg',
}
]
}
renderItem = ({ item }) => {
return (
<Image source={{ uri: item }} style={styles.logoStyle} />
);
}
<View>
<Carousel
inactiveSlideOpacity={0.6}
inactiveSlideScale={0.65}
firstItem={1}
sliderWidth={width}
itemWidth={width / 3}
data={this.state.categoriesimages['images']} // this is the problem
renderItem={this.renderItem}
containerCustomStyle={{ overflow: 'visible' }}
contentContainerCustomStyle={{ overflow: 'visible' }}
/>
</View>
我还是 React Native 的新手,查找类似的问题并没有找到正确的答案。
您可以为此使用地图 this.state.categoriesimage.map(value => value.image)
此处this.state.categoriesimages['images']
,data参数接受array
格式。
现在您正在尝试访问索引为 images
categoriesimages
因此您需要将其替换为
data={this.state.categoriesimages}
和
renderItem={({item}) => console.log(item.images)}