【发布时间】:2018-06-18 07:17:50
【问题描述】:
我正在使用列表视图。在该列表中,我可以删除任何记录,但删除后我希望 listview 刷新并显示新记录,这意味着我想调用我的 componentDidMount()。我正在使用 navigation.navigate() 但它没有调用 componentDidMount()。下面是我的代码 sn-p 供参考。
import React, { Component } from 'react';
import { AppRegistry, StyleSheet, Keyboard , TouchableWithoutFeedback ,TextInput, ActivityIndicator, Picker, Switch, ListView,
Text, View, Alert, Platform, ToastAndroid, TouchableHighlight, Image, Button, TouchableOpacity, ScrollView } from 'react-native';
import EvilIcon from 'react-native-vector-icons/EvilIcons';
import MaterialIcon from 'react-native-vector-icons/MaterialIcons';
import { StackNavigator } from 'react-navigation';
import { NavigationActions } from 'react-navigation'
import Toast from 'react-native-simple-toast';
class manage_timeslots extends Component
{
static navigationOptions = {
title: 'Manage Timeslots',
};
constructor(props) {
super(props);
this.state = {
isLoading: true,
text: '',
stat: '',
}
this.arrayholder = [];
}
componentDidMount() {
const base64 = require('base-64');
this._subscribe = this.props.navigation.addListener('didFocus', () =>
{ console.log("in focus")
fetch('APIURL'+this.props.navigation.state.params.campaign_id, {
method: 'GET',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
"Authorization": "Basic " + base64.encode("demo:QZMWOL")
}
})
.then((response) => response.json())
.then((responseJson) => { console.log("timeslots: "+ JSON.stringify(responseJson));
let ds = new ListView.DataSource({ rowHasChanged: (r1, r2) => r1 !== r2 });
this.setState({
isLoading: false,
data : "data",
dataSource: ds.cloneWithRows(responseJson.Time_Slots),
}, function () { //console.log("timeslots: " + this.state.dataSource);
});
})
.catch((error) => {
// Alert.alert(error.toString())
this.setState({
isLoading: false,
data: "noDataFound"
})
});
});
}
onDeleteTimeSlot(Timeslot_id) {
const base64 = require('base-64');
fetch('APIURL'+Timeslot_id, {
method: 'DELETE',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
"Authorization": "Basic " + base64.encode("demo:QZMWOL")
}
})
.then((response) => response.json())
.then((responseJson) => { console.log("response: " + JSON.stringify(responseJson));
if(responseJson.Message == 'Data has been deleted successfully!')
{ console.log("here");
this.props.navigation.navigate('Eighth', { campaign_id: this.props.navigation.state.params.campaign_id, ClientId: this.props.navigation.state.params.ClientId, Ad_name: this.props.navigation.state.params.Ad_name } );
}
else
{
console.log(responseJson);
}
}).catch((error) => {
console.error(error);
});
}
}
从 onDeleteTimeSlot() 我需要再次调用 componentDidmount() 函数来刷新数据源以获取新记录。
【问题讨论】:
-
将componentDidMount中的代码移动到新方法中,从componentDidMount中调用,删除后也一样
标签: react-native react-navigation stack-navigator