如何使用Stack Actions在反应导航中重置路线?

时间:2019-02-16 18:09:38

标签: javascript react-native react-navigation

男孩和女孩,我该如何使用Stack Actions在react-navigation中重置路由并使用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。现在stackaction可以工作了,但是在WorkersView中,我遇到了这个错误:

警告:无法在已卸载的组件上调用setState(或forceUpdate)。这是空操作,但它表明应用程序中发生内存泄漏。要修复此问题,请取消componentWillUnmount方法中的所有订阅和异步任务。

1 个答案:

答案 0 :(得分:0)

您正在尝试同时进入两个不同的屏幕,即屏幕createWorkerWorkers。只要做:
const resetAction = StackActions.reset({ index: 0, actions: [NavigationActions.navigate({ routeName: 'createWorker' })], }); this.props.navigation.dispatch(resetAction);