【发布时间】:2020-07-24 12:38:47
【问题描述】:
我有一个杂志风格的应用程序,它有一个索引页面 - 名为 HomeScreen 的组件 - (带有文章列表)和一个详细页面 - 名为 Details 的组件 - (带有完整文章); 在它们上面我有一个 Home 组件,它是 bot 的 Stack 导航。
首页组件
const Home = ({}) => {
return (
<Stack.Navigator initialRouteName="Home">
<Stack.Screen name="Home" component={HomeScreen} options={HeaderOption} />
<Stack.Screen
name="Details"
component={DetailsScreen}
options={HeaderOption}
/>
</Stack.Navigator>
);
};
export default Home;
然后(同一个文件)获得 HomeScreen 组件,该组件(正确)呈现文章列表,它使用 useEffect 从 API 获取它们
function HomeScreen({ navigation }) {
const [article, setArticles] = useState();
const [loadingArticle, setLoading] = useState(false);
useEffect(() => {
console.log('start Fetching');
fetchTitles();
}, []);
const fetchTitles = () => {
fetch('myURL', {
method: 'GET',
})
.then((response) => response.json())
.then((responseData) => {
setArticles(responseData);
})
.finally(() => setLoading(false));
};
const { state } = useContext(AuthContext);
return (
<View
style={{
flex: 1,
paddingTop: Constants.HEIGTH,
backgroundColor: Constants.MAIN_GREEN,
}}
>
<LogoHeader title="TITOLO APP" isHome={true} navigation={navigation} />
<View style={{ ...styles.container }}>
<FlatList
data={article}
keyExtractor={(item) => item.id.toString()}
renderItem={({ item }) => (
<TouchableOpacity
onPress={() =>
navigation.navigate('Details', {
itemId: item.id,
})
}
>
<Card>
<Card.Content>
<HTML
html={item.title.rendered}
baseFontStyle={{ fontSize: 20, color: '#adadad' }}
/>
<Paragraph>
Published on {moment(item.date).format('LL')}
</Paragraph>
</Card.Content>
<Card.Cover source={{ uri: item.jetpack_featured_media_url }} />
<Card.Content>
<HTML
html={item.excerpt.rendered}
imagesMaxWidth={Dimensions.get('window').width}
/>
</Card.Content>
</Card>
</TouchableOpacity>
)}
/>
</View>
</View>
);
}
当我点击文章时,我使用正确传递的 id 正确到达 Details,但 singleArticle 为“null”,即使 responseData 正确:
function DetailsScreen({ route, navigation }) {
const { state } = useContext(AuthContext);
const [singleArticle, setSingleArticle] = useState(null);
const [loadingArticle, setLoadingSingle] = useState(false);
useEffect(() => {
setLoadingSingle(true);
console.log(loadingArticle); //return false (???)
fetchSingle();
}, []);
const fetchSingle = () => {
console.log('get_single');
fetch(
'myURLgetsingle?' +
route.params.itemId,
{
method: 'GET',
}
)
.then((response) => response.json())
.then((responseData) => {
console.log('setting');
console.log(responseData); //return the values i need
setSingleArticle(responseData);
console.log(singleArticle); //return null
})
.finally(() => setLoadingSingle(false));
};
console.log(singleArticle.title.rendered);
return (
<View
style={{
flex: 1,
paddingTop: Constants.HEIGTH,
backgroundColor: Constants.MAIN_GREEN,
}}
>
<LogoHeader title="" isHome={false} navigation={navigation} />
<Card style={{ ...styles.container, padding: 10 }}>
<Card.Content>
<Title>{singleArticle.title.rendered}</Title>
<Paragraph></Paragraph>
</Card.Content>
</Card>
</View>
);
}
我想我错过了 react native 的一些基本用法,有什么办法可以修复它吗? 谢谢
【问题讨论】:
标签: react-native fetch