javascript - React Native 应用程序可以在 iOS 上运行,但在 Android 上出现错误 posts.map 未定义
问题描述
我创建了一个简单的程序来在应用程序上显示来自我的网站的信息。它适用于 iOS,但在我的 Android 设备上显示错误:posts.map 不是函数。这是我的代码:
import React, { Component } from 'react';
import {
View,
Text,
ActivityIndicator,
ScrollView,
StyleSheet,
Image,
Header,
} from 'react-native';
let lw = 100;
import Img from 'react-image';
let li = 'https://www.teanewsnetwork.com/profileicons/';
let bean = 'azure.jpg';
export default class App extends Component {
state = {
loading: true,
error: false,
posts: [],
};
componentWillMount = async () => {
try {
const response = await fetch(
'https://www.teanewsnetwork.com/api/fetcharticles.php?code=#NIL7*GKD60JTRTEFZ0CkvpHMJJW^-9q&starting=0&limit=40'
);
const posts = await response.json();
this.setState({ loading: false, posts });
} catch (e) {
this.setState({ loading: false, error: true });
}
};
renderPost = ({ id, title, content, authorimage, authorname }, i) => {
let b = { authorname };
return (
<View style={styles.postContent}>
<View
style={{
flex: 1,
flexDirection: 'column',
textAlign: 'center',
justifyContent: 'center',
alignItems: 'center',
}}>
<Text style={styles.postauthor}>{title} </Text>
<Image
source={{
uri: `https://teanewsnetwork.com/profileicons/${authorimage}`,
}}
defaultSource={require('./contact-outline.png')}
style={{
width: lw,
height: lw,
flex: 1,
justifyContent: 'center',
alignItems: 'center',
borderRadius: lw / 2,
}}
/>
<Text style={styles.postauthor}>{authorname}</Text>
</View>
<Text style={styles.postBody}>{content}</Text>
</View>
);
};
render() {
const {posts, loading, error} = this.state
if (loading) {
return (
<View style={styles.center}>
<ActivityIndicator animating={true} />
</View>
)
}
if (error) {
return (
<View style={styles.center}>
<Text>
Failed to load posts!
</Text>
</View>
)
}
return (
<ScrollView style={styles.container}>
{posts.map(this.renderPost)}
</ScrollView>
)
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
borderBottomWidth: 0,
borderBottomColor: 'red',
top: 100,
zIndex: 6,
},
postauthor: {
flex: 1,
borderBottomWidth: 0,
borderBottomColor: 'red',
paddingVertical: 25,
fontSize: 18,
paddingRight: 15,
left: 10,
justifyContent: 'center',
alignItems: 'center',
textAlign: 'center',
},
postContent: {
flex: 1,
borderBottomWidth: 20,
borderBottomColor: '#EEE',
borderRadius: 4,
fontSize: 18,
left: 0,
paddingRight: 15,
justifyContent: 'center',
alignItems: 'center',
textAlignVertical: 'center',
textAlign: 'center',
},
postBody: {
marginTop: 1,
fontSize: 18,
color: 'black',
left: 10,
textAlign: 'center',
},
center: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
},
});
我是 react native 的初学者编码器,希望在这里得到一些帮助。该代码适用于带有 EXPO 的 iOS 模拟器,但不适用于我的 Android 设备。提前致谢!
解决方案
很可能posts
不是数组,请检查posts
.
推荐阅读
- ibm-cloud-infrastructure - 使用 reloados REST API 重新加载不同的操作系统
- react-native - 如何在应用程序打开时渲染堆栈屏幕而不将其放入抽屉屏幕
- vb.net - 如何在发布后替换资源 .txt 文件而不会出错
- javascript - 将拖放内容保存到 html 中的 localStorage
- android - 当 LinearLayout 移动到另一个地方时,按钮丢失 onClickListener
- javascript - 画外音不是阅读输入的标签,只是内容 - safari
- javascript - 如果另一个单元格的颜色发生更改,则在 Google 表格中为一个单元格添加时间戳
- javascript - 在页面加载时运行 chrome 扩展
- regex - 将大写 URL 重定向到特定于子文件夹的小写版本
- c - 如何在二进制字符串中执行遗传算法的利用?