我收到此错误
TypeError:null不是一个对象(正在评估“ dataSource.map”)
我看过其他解决方案,但我不明白为什么setDatasource不会将dataSource从null更改。
import React, { Component, useState, useEffect } from "react";
import { View, Text, StyleSheet, ActivityIndicator } from "react-native";
import { ScreenContainer } from "react-native-screens";
export const Home = () => {
const [isLoading, setisLoading] = useState(true);
const [dataSource, setdataSource] = useState(null);
useEffect(() => {
return fetch("https://facebook.github.io/react-native/movies.json")
.then((response) => response.json())
.then((responseJson) => {
setisLoading(false), setdataSource(responseJson.movie);
});
// .catch((error)) => {
// console.log(error)
// }
});
if (isLoading) {
return (
<View>
<ActivityIndicator />
</View>
);
} else {
let moviest = dataSource.map((val, key) => {
return (
<View key={key}>
<Text>{val.title}</Text>
</View>
);
});
return (
<ScreenContainer style={styles.container}>
<View>
<Text>These are the movies</Text>
{moviest}
</View>
</ScreenContainer>
);
}
};
collections.Counter
(doc)用于任务: