2017-12-10 6 views
0

私の反応ネイティブプロジェクトにイメージスライダーを作成しようとしていますが、なぜ私にこのエラーを送信し続けているのか分かりません:ReferenceError:Can not変数を見つける:私が次のコンストラクタを持っているときにpropsを呼び出してレンダリングする。変数を見つけることができません:props(react-native)

constructor(props){ 
super(props) 
    this.state = { 
    imagesSlider: [ 
    require('../images/1.jpg'), 
    require('../images/2.jpg') 
    ] 
    } 
} 

render() { 
return (
    <Swiper style={styles.slide} autoplay> { 
    this.state.imagesSlider.map(function(item,i){ 
    <View key={i}> 
    <Image source={props.item}/> 
    </View> 
    }) 
    } 
    </Swiper> 
); 
} 
+0

私を助けてくれますか? :) – Tekla

答えて

2

あなたは 'this'を使ってあなたのpropsと状態変数を参照するべきです。あなたは小道具は、この文脈の中で利用できるようになります代わりに、直接小道具にアクセスすることはできません

<Image source={this.props.item}/> 
+0

TypeError:undefinedはオブジェクトではありません( 'this.props.item'を評価しています):( – Tekla

1

:だからあなたにに変更する必要があります。したがって、this.propsを使用して、propsを直接使用するのではなく、propsから値を取得してください。あなたはあなたの解決のために次のコードを変更する必要があります: -

render() { 
return (
    <Swiper style={styles.slide} autoplay> { 
    this.state.imagesSlider.map(function(item,i){ 
    <View key={i}> 
    <Image source={this.props.item}/> ===> this needs to be changed to get the value of item from props. 
    </View> 
    }) 
    } 
    </Swiper> 
); 

UPDATE: -

をコメントによると、あなたがからアイテムを取得するのではなく、状態変数のマップからアイテムを使用している場合、小道具の場合、コードを以下のように変更する必要があります: -

render() { 
return (
    <Swiper style={styles.slide} autoplay> { 
    this.state.imagesSlider.map(function(item,i){ 
    <View key={i}> 
    <Image source={item}/> ===> this needs to be changed to get the value of item from the map of state variable. 
    </View> 
    }) 
    } 
    </Swiper> 
); 
+0

TypeError:未定義はオブジェクトではありません( 'this.props.item'を評価しています) – Tekla

+0

propsのアイテムを使用している場合は、 –

+0

エラーメッセージ:YogaNodeを持たない子を追加できません。測定機能なしの親に! – Tekla

関連する問題