如何显示 activity 指示器,直到使用 redux 从 firestore 中获取并显示在反应本机中的所有数据
How to show activity indicator until all data from firestore are fetched and displayed in react native using redux
我在使用 Redux 的反应本机应用程序中使用 FireBase 作为数据库。在获取数据之前,我想显示一个 Activity 指示器。我的 Redux 操作是:
export const fetchproducts=()=>{
return async dispatch=>{
const items = []
const db = firebase.firestore();
db.collection("Shops").collection('products').get().then((querySnapshot)=> {
querySnapshot.forEach((doc,i)=> {
items.push(new Product(
doc.id,
doc.data().name,
doc.data().description,
doc.data().price,
))
});
dispatch({
type:SET_PRODUCTS,
products:items
})
}).catch((err)=>{
throw err
})
}
}
我的 React Native js 代码是:
const ProductsOverviewScreen = props => {
const [isLoading,setIsLoading]=useState(false)
const [isRefreshing,setIsRefreshing]=useState(false)
const allshops = useSelector(state => state.shops.allshops);
const [error,setError]=useState()
const products = useSelector(state => state.products.availableProducts);
const dispatch = useDispatch();
const loadProducts=useCallback( async ()=>{
setError(null)
setIsLoading(true)
try{
await dispatch(productActons.fetchproducts())
}catch(err){
setError(err.message)
}
setIsLoading(false)
},[dispatch,setIsLoading,setError])
useEffect(()=>{
const willFocusSub=props.navigation.addListener('willFocus',()=>{
loadProducts()
})
return ()=>{
willFocusSub.remove()
}
},[loadProducts])
useEffect(()=>{
loadProducts()
},[dispatch,loadProducts])
if(error)
{
return <View style={{flex:1,alignItems:'center',justifyContent:'center'}}>
<Text>An error Occured </Text>
<Button title="Try Again" onPress={loadProducts} color={Colors.primary}></Button>
</View>
}
if(isLoading){
return <View style={{flex:1,alignItems:'center',justifyContent:'center'}}>
<ActivityIndicator size="large" color={Colors.primary}/>
</View>
}
if(!isLoading&&products.length===0)
{
return <View style={{flex:1,alignItems:'center',justifyContent:'center'}}>
<Text>No Products</Text></View>
}
return (
<View>
<FlatList
horizontal={true}
data={products}
keyExtractor={item => item.id}
renderItem={itemData => (
<ProductItem
image={itemData.item.id}
title={itemData.item.name}
price={itemData.item.price}
onSelect={() => {
selectItemHandler(itemData.item.id, itemData.item.title);
}}
>
</ProductItem>
)}
/>
</View>
输出先显示'No Products'再显示产品,而不是先显示activity指标再显示产品
改变
if(isLoading){
return <View style={{flex:1,alignItems:'center',justifyContent:'center'}}>
<ActivityIndicator size="large" color={Colors.primary}/>
</View>
}
if(!isLoading&&products.length===0)
{
return <View style={{flex:1,alignItems:'center',justifyContent:'center'}}>
<Text>No Products</Text></View>
}
至
if(!isLoading && products.length===0){
return <View style={{flex:1,alignItems:'center',justifyContent:'center'}}>
<ActivityIndicator size="large" color={Colors.primary}/>
</View>
}
if(isLoading&&products.length===0)
{
return <View style={{flex:1,alignItems:'center',justifyContent:'center'}}>
<Text>No Products</Text></View>
}
希望对您有所帮助!
我在使用 Redux 的反应本机应用程序中使用 FireBase 作为数据库。在获取数据之前,我想显示一个 Activity 指示器。我的 Redux 操作是:
export const fetchproducts=()=>{
return async dispatch=>{
const items = []
const db = firebase.firestore();
db.collection("Shops").collection('products').get().then((querySnapshot)=> {
querySnapshot.forEach((doc,i)=> {
items.push(new Product(
doc.id,
doc.data().name,
doc.data().description,
doc.data().price,
))
});
dispatch({
type:SET_PRODUCTS,
products:items
})
}).catch((err)=>{
throw err
})
}
}
我的 React Native js 代码是:
const ProductsOverviewScreen = props => {
const [isLoading,setIsLoading]=useState(false)
const [isRefreshing,setIsRefreshing]=useState(false)
const allshops = useSelector(state => state.shops.allshops);
const [error,setError]=useState()
const products = useSelector(state => state.products.availableProducts);
const dispatch = useDispatch();
const loadProducts=useCallback( async ()=>{
setError(null)
setIsLoading(true)
try{
await dispatch(productActons.fetchproducts())
}catch(err){
setError(err.message)
}
setIsLoading(false)
},[dispatch,setIsLoading,setError])
useEffect(()=>{
const willFocusSub=props.navigation.addListener('willFocus',()=>{
loadProducts()
})
return ()=>{
willFocusSub.remove()
}
},[loadProducts])
useEffect(()=>{
loadProducts()
},[dispatch,loadProducts])
if(error)
{
return <View style={{flex:1,alignItems:'center',justifyContent:'center'}}>
<Text>An error Occured </Text>
<Button title="Try Again" onPress={loadProducts} color={Colors.primary}></Button>
</View>
}
if(isLoading){
return <View style={{flex:1,alignItems:'center',justifyContent:'center'}}>
<ActivityIndicator size="large" color={Colors.primary}/>
</View>
}
if(!isLoading&&products.length===0)
{
return <View style={{flex:1,alignItems:'center',justifyContent:'center'}}>
<Text>No Products</Text></View>
}
return (
<View>
<FlatList
horizontal={true}
data={products}
keyExtractor={item => item.id}
renderItem={itemData => (
<ProductItem
image={itemData.item.id}
title={itemData.item.name}
price={itemData.item.price}
onSelect={() => {
selectItemHandler(itemData.item.id, itemData.item.title);
}}
>
</ProductItem>
)}
/>
</View>
输出先显示'No Products'再显示产品,而不是先显示activity指标再显示产品
改变
if(isLoading){
return <View style={{flex:1,alignItems:'center',justifyContent:'center'}}>
<ActivityIndicator size="large" color={Colors.primary}/>
</View>
}
if(!isLoading&&products.length===0)
{
return <View style={{flex:1,alignItems:'center',justifyContent:'center'}}>
<Text>No Products</Text></View>
}
至
if(!isLoading && products.length===0){
return <View style={{flex:1,alignItems:'center',justifyContent:'center'}}>
<ActivityIndicator size="large" color={Colors.primary}/>
</View>
}
if(isLoading&&products.length===0)
{
return <View style={{flex:1,alignItems:'center',justifyContent:'center'}}>
<Text>No Products</Text></View>
}
希望对您有所帮助!