接下來,分享的是要如何再載入頁面將Menu從API Server讀取回來顯示在頁面列表
const [menu, setAllMenu] = useState([]);
const [refreshing, setRefreshing] = useState(false);
const onRefresh = () => {
setRefreshing(true);
handleClickGetMenu();
};
<FlatList
data={menu}
renderItem={renderItem}
keyExtractor={item => item.id.toString()}
refreshControl={
<RefreshControl refreshing={refreshing} onRefresh={onRefresh} />
}
/>
const handleClickGetMenu = () => {
axios
.get(
'https://json-server-vercel-w33n-git-main-raychen1996.vercel.app/Menus',
)
.then(response => {
console.log(response.data);
setAllMenu(response.data);
setIsLoading(false);
setRefreshing(false);
})
.catch(error => {
setIsLoading(false);
});
};