我想使用react-native-snap-carousel,但是当我尝试init in时,我有一个错误:(
下面是一个例子:
import Carousel from 'react-native-snap-carousel';
export class MyCarousel extends Component {
_renderItem ({item, index}) {
return (
<View style={styles.slide}>
<Text style={styles.title}>{ item.title }</Text>
</View>
);
}
render () {
return (
<Carousel
ref={(c) => { this._carousel = c; }}
data={this.state.entries}
renderItem={this._renderItem}
sliderWidth={sliderWidth}
itemWidth={itemWidth}
/>
);
}}我的代码:
import React from 'react';
import { StyleSheet, Text, View } from 'react-native';
import Carousel from 'react-native-snap-carousel';
export default class App extends React.Component {
_renderItem ({item, index}) {
return (
<View style={styles.slide}>
<Text style={styles.title}>{ item.title }</Text>
</View>
);}
render () {
return (
<Carousel
ref={(c) => { this._carousel = c; }}
data={this.state.entries}
renderItem={this._renderItem}
sliderWidth={150}
itemWidth={100}
/>
);
}}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
}});在app.js react本机上使用相同的Screenshot
我看到了一个问题(和我一样) link to Github Issue
但不回答和问题很接近
发布于 2018-08-21 16:56:09
正如截图所示,this.state.entries是null。
您必须初始化它:
export default class App extends React.Component {
constructor() {
super()
this.state = {
entries: [],
}
}
_renderItem ({item, index}) {
return (
<View style={styles.slide}>
<Text style={styles.title}>{ item.title }</Text>
</View>
);}
render () {
return (
<Carousel
ref={(c) => { this._carousel = c; }}
data={this.state.entries}
renderItem={this._renderItem}
sliderWidth={150}
itemWidth={100}
/>
);
}}在本例中,entries: []不会显示任何内容,因为其中没有对象。您可以使用所需的数据对其进行初始化:
entries: [
{ title: 'hello' },
{ title: 'world' },
]顺便说一句,这个问题与插件本身无关,即使他们可以捕捉到它。
https://stackoverflow.com/questions/51944706
复制相似问题