每次屏幕以本机安装时,会调用哪个组件生命周期?

时间:2018-10-01 07:34:21

标签: javascript reactjs react-native expo

我要从Firebase带上图像。第一次打开主屏幕时,我的应用程序运行良好,但是当我转到其他屏幕并返回主屏幕时,仅加载一个图像,这是Firebase中的最后一个图像。我唯一的目的是从Firebase提取图像,并通过平面列表在主页中显示图像。第一次发生这种情况,但是当我通过抽屉导航到其他屏幕并再次返回时,仅从firebase中调用了最后一个img。

 export default class Home extends React.Component {

      constructor(props) {
        super(props);

        this.state = {
          backgroundImageUrl : [],
        }
      }



      static navigationOptions = {
        header: null ,
        headerForceInset: {vertical: 'never'},
      };



    componentWillMount()
      {
        firebase.database().ref().child("Card_Images/").on("child_added", function(snapshot) {


          this.setState({
            backgroundImageUrl : [...this.state.backgroundImageUrl, snapshot.val()],
          })
        }.bind(this))

      }





        handleTap = () => {
          //  console.log("item", item);this.props.screenProps.navigation.navigate("ItemDetails",
          this.props.navigation.navigate("ItemDetails")};

        renderItem = ({ item }) => {
          return (

            <TouchableOpacity style={styles.itemContainer}
            onPress = {this.handleTap}>

            <View style = {{height:180,width:Dimensions.get('window').width-32,marginLeft:16,marginRight:16,backgroundColor: 'black',marginTop: 16,marginBottom:16,
         borderRadius: 16}}>

           <Image
                        style={{
                         width:Dimensions.get('window').width-32,
                         height:160,
                         resizeMode:"cover",
                         alignSelf: 'center',
                         borderRadius:8,

                        }}
                        source = {{ uri: item }}
                        //onPress = {() => this.selectCity()}
                      />

        <View style = {styles.DesignInformation}>
                    <Text style={{color:'grey',fontSize: 10,fontWeight:"bold",marginLeft:2}}> Rishavh Gupta </Text>
                    <Text style={{color:'grey',textAlign:"right",fontSize: 10,fontStyle: 'italic',marginRight:8,
                    shadowOffset: { width: 5, height: -1 }, elevation:4,borderBottomColor:'white',borderBottomWidth:0.8,shadowColor: "gray",shadowOpacity: 0.5,shadowRadius: 4}}>www.cometgraphic.com </Text>

        </View>

                    </View>
        </TouchableOpacity>
          );
        };

      render() {
        console.log("images are", this.state.backgroundImageUrl)
        // console.log("sd", this.state.backgroundImageUrl)


        return (
                <View style = {styles.container}>


                <FlatList 
          data={this.state.backgroundImageUrl}
          renderItem={this.renderItem}
          keyExtractor={item => item.id}
          numColumns={numColumns}
           />

1 个答案:

答案 0 :(得分:1)

要调试该问题,我想知道第二次图像数组包含的条目数 或者,您可以将列表保存在您的redux存储中,而不是使其保持组件状态。