如果使用Redux从Firebase提取数据,如何显示活动指示器

时间:2019-09-30 08:02:47

标签: javascript firebase react-native redux axios

我正在使用FireBase作为数据库,以使用Redux在react-native应用程序中获取数据。我想显示一个活动指示器,直到获取数据为止。 这是我的代码Redux:

export function getHome() {
  const request = axios({
    method: "GET",
    url: `${FIREBASEURL}/home.json`
  })
    .then(response => {
      const articles = [];
      for (let key in response.data) {
        articles.push({
          ...response.data[key],
          id: key
        });
      }

      return articles;
    })
    .catch(e => {
      return false;
    });

  return {
    type: GET_HOME,
    payload: request
  };
}

这是我的React Native代码,其中将显示数据:

import React, { Component } from "react";
import {
  StyleSheet,
  View,
  Text,
  ScrollView,
  ActivityIndicator,
  TouchableWithoutFeedback,
  Image
} from "react-native";
import { connect } from "react-redux";
import { getHome } from "../store/actions/home_actions";
import DemoScreen from "./rn-sound/demo";

class HomeScreen extends Component {
  componentDidMount() {
    this.props.dispatch(getHome());
  }
  renderArticle = imgs =>
    imgs.articles
      ? imgs.articles.map((item, i) => (
          <TouchableWithoutFeedback
            onPress={() => this.props.navigation.navigate(`${item.navigate}`)}
            key={i}
          >
            <View>
              <View>
                <Image
                  style={{
                    height: 220,
                    width: "100%",
                    justifyContent: "space-around"
                  }}
                  source={{ uri: `${item.image}` }}
                  resizeMode="cover"
                />
              </View>
              <View>
                <Text >{item.name}</Text>
              </View>
              <View>
                <Text }>{item.tagline}</Text>
              </View>
            </View>
          </TouchableWithoutFeedback>
        ))
      : null;

  render() {
      return (
        <ScrollView}>
          {this.renderArticle(this.props.Home)}
        </ScrollView>
      );
  }
}

如何显示Activity Indiactor,直到从Firebase提取了我的数据

1 个答案:

答案 0 :(得分:0)

您可以在状态下使用加载变量。在设置为true之后,在获取命令之前将其设置为false。您可以在下面看到示例。

constructor(props) {
    super(props);

    this.state = {
      loading: false
    };
  }

componentDidMount = () => {
this.setState({
    loading: true
})
this.props.dispatch(getHome()).then(response=>{
    this.setState({
        loading: false
    })
})
}

render() {
      return (
        <ScrollView}>            
          {this.state.loading == false ? (
          <View>
            {this.renderArticle(this.props.Home)}
          </View>
        ) : (
          <ActivityIndicator size="large" />
        )}
        </ScrollView>
      );
  }