男孩和女孩,我如何通过堆栈动作重置反应路线并使用componentDidupdate而不是componentdidmount。
可能吗?
这是我的代码-WorkersView:
import React from 'react';
import { Text, FlatList, View, TouchableOpacity, StatusBar } from 'react-native';
import { Container, Content, ListItem, List, Icon, Fab, Header, Body, Left, Title, Button, Right } from 'native-base';
import { custom, responsive } from '../../styles/config';
import { AntDesign, FontAwesome, MaterialCommunityIcons } from '../../styles/variables/Icons'
import { openDatabase } from 'react-native-sqlite-storage';
var db = openDatabase({ name: 'Database.db' });
export default class WorkersView extends React.Component {
_isUpdated= false;
constructor(props) {
super(props);
this.state = {
data: [],
error: null,
isLoading: true
};
this.arrayholder = [];
this.getAllWorkers();
}
componentDidUpdate() {
this.getAllWorkers();
}
这是我的代码-CreateWorker:
onButtonPress() {
// const navigateAction = NavigationActions.navigate({
// routeName: 'Workers',
// params: {},
// action: NavigationActions.navigate({ routeName: 'WorkersView' }),
// });
// this.props.navigation.dispatch(navigateAction);
const resetAction = StackActions.reset({
index: 0,
actions: [NavigationActions.navigate({ routeName: 'createWorker' })],
});
const goHome = NavigationActions.navigate({
routeName: 'Workers',
params: {}
})
this.props.navigation.dispatch(resetAction);
this.props.navigation.dispatch(goHome);
}
我不想使用componentDidmount我想使用componentDidupdate。现在堆叠工作,但在WorkersView中我有一个错误:
警告:无法在未数组件上调用SetState(或ForceupDate)。这是一个无操作,但表示您的应用程序中的内存泄漏。要修复,请在ComponentWillunMount方法中取消所有订阅和异步任务。
您正在尝试同时转到2个不同的屏幕,即屏幕createWorker
和Workers
。只是做:
const resetAction = StackActions.reset({
index: 0,
actions: [NavigationActions.navigate({ routeName: 'createWorker' })],
});
this.props.navigation.dispatch(resetAction);