this.props.data.map正在获取未定义的错误

时间:2018-06-08 06:55:49

标签: react-native

执行代码时

export default class Slides extends React.Component {
  renderSlides() {
    return this.props.data.map((slide)=> {
      return (
        <View key={slide.text}>
          <Text> {slide.text} </Text>
        </View>
      );
    });
  }

  render() {
    return (
      <ScrollView
        horizontal
        style= {{ flex: 1 }}
      >
        { this.renderSlides() }
      </ScrollView>
    );
  }
}

错误为this.props.map() error

1 个答案:

答案 0 :(得分:0)

首先,您需要将数据传递给幻灯片组件

 import React from 'react';
 import { Text, View } from 'react-native';
 import Slides from "../components/Slides";
 const SLIDE_DATA = [ { text: 'Welcome to JobApp' }, { text: 'Use this to get a Job' }, { text: 'Set your location, then swipe away' } ] 

 export default class WelcomeScreen extends React.Component {
   render() { 
      return(
          <Slides data = {SLIDE_DATA}/> 
      ); 
     } 
} 

您可以使用Slides组件中的this.props.data获取数据