【问题标题】:Which component life cycle gets called everytime the screen mounts in react native?每次屏幕以 React Native 方式挂载时,都会调用哪个组件生命周期?
【发布时间】:2018-10-01 07:34:21
【问题描述】:

我从 firebase 带来图片。我的主屏幕第一次打开我的应用程序工作正常,但是当我转到其他屏幕并返回主屏幕时,只有一个图像被加载,这是 firebase 中的最后一个图像。我的唯一目的是从 firebase 中获取图像并通过 flatlist 在主页上显示图像。这是第一次发生,但是当我通过抽屉导航到其他屏幕并再次返回时,只会从 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}
           />

【问题讨论】:

  • 您需要维护历史记录。
  • 历史,怎么样?能否请您详细说明我很困惑。

标签: javascript reactjs react-native expo


【解决方案1】:

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

【讨论】:

  • 暂时我在firebase中的图像总数为8..第一次它显示总共8张图像,但从一个导航到另一个导航并返回后它只显示一个图像......
  • 它是否会在第二个实例中获取 8 张图像作为响应。您可能会在解构响应时覆盖旧条目并存储最后一个条目。
  • 我解决了它,这是一个简单的错误。我将数组中的值传递给该数组,然后将该数组作为值来设置状态。
  • :D 那太好了
猜你喜欢
  • 1970-01-01
  • 2021-11-25
  • 1970-01-01
  • 2018-05-21
  • 1970-01-01
  • 2023-03-24
  • 1970-01-01
  • 2020-03-05
  • 1970-01-01
相关资源
最近更新 更多