Javascript 每次在react native中安装屏幕时,哪个组件生命周期被调用?
我带来了来自firebase的图像。第一次打开主屏幕时,我的应用程序工作正常,但当我转到其他屏幕并返回主屏幕时,只加载了一个图像,这是firebase中的最后一个图像。我的唯一目的是将firebase的图像通过flatlist显示在主页上。在第一次发生时,但当我通过抽屉导航到其他屏幕并再次返回时,只有最后一个img从firebase调用Javascript 每次在react native中安装屏幕时,哪个组件生命周期被调用?,javascript,reactjs,react-native,expo,Javascript,Reactjs,React Native,Expo,我带来了来自firebase的图像。第一次打开主屏幕时,我的应用程序工作正常,但当我转到其他屏幕并返回主屏幕时,只加载了一个图像,这是firebase中的最后一个图像。我的唯一目的是将firebase的图像通过flatlist显示在主页上。在第一次发生时,但当我通过抽屉导航到其他屏幕并再次返回时,只有最后一个img从firebase调用 export default class Home extends React.Component { constructor(props) {
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}
/>
为了调试这个问题,我想知道您的图像数组第二次包含的条目数
或者,您可以将列表保存在redux存储中,而不是将其保持在组件状态 你需要保存历史。历史,如何保存?你能详细说明一下吗?我很困惑。暂时我在firebase中的图片总数是8。。第一次它总共显示了8幅图像,但从一个导航到另一个导航,然后返回,它只显示了一幅图像……它正在第二次获取8幅图像作为响应。在分解响应时,您可能会覆盖旧条目并存储最后一个条目。不过我解决了这个问题,这只是一个简单的错误。。我在数组中传递了值,然后将该数组作为值来设置状态。:D那太好了