react-native - this.props.data.map 出现未定义错误
问题描述
在执行代码时
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>
);
}
}
解决方案
首先,您需要将数据传递给 Slides 组件
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}/>
);
}
}
您可以使用幻灯片组件中的 this.props.data 获取数据
推荐阅读
- firebase - 如何在 Flutter 中跟踪屏幕视图并将其显示在应用程序中
- python - Asyncio.create_subprocess_exec NotImplementedError - Fastapi 后台任务
- math - 提取旋转值并将其添加到 Maya 矩阵
- android - 当我从最后一个片段返回时如何避免活动的空白屏幕
- javascript - 如何在单词数组中找到一个句子并将这些单词连接起来
- python - python请求分块编码文件和元数据
- uwp-xaml - SelectedItem 的 C++ WinRT UWP XAML 数据绑定到 String / hstring 类型的属性无法编译
- css - 如何通过flex解决以下查询?
- maven - Maven 是否具有 Gradle 的“编译项目”等功能?
- jquery - Wordpress JQuery 将变量内容作为可点击链接发送到电子邮件正文