【发布时间】:2026-02-19 18:05:01
【问题描述】:
import React from 'react';
import { Card, Text } from "react-native-paper";
import {
SafeAreaView,
} from "react-native";
class Hnews extends React.Component {
constructor(props) {
super(props);
this.state = {
data: [],
isLoaded: false,
};
}
// Function to collect data
async componentDidMount() {
await fetch(
"https://hacker-news.firebaseio.com/v0/item/26061935.json?print=pretty"
)
.then((res) => res.json())
.then((json) => {
this.setState({
isLoaded: true,
data: json.data,
});
console.log("Json", JSON.stringify(json));
});
}
render() {
return (
<SafeAreaView>
<Card>
{this.state.newdata.map((item) => {
<Text>{item.title}</Text>
})}
</Card>
</SafeAreaView>
);
}
}
export default Hnews;
我正在做一个黑客新闻克隆,我正在获取 API 并显示它,但给出了错误“无法读取未定义的属性(读取 'map')”。我已经看到了很多此类问题的答案,并更改了我的解决方案,但没有任何效果。
【问题讨论】:
-
什么时候调用 someMethod ?
-
我做了console.log,它什么也没给我。我删除了它。更新了帖子。
标签: javascript arrays reactjs react-native api