如何使用Stack Actions在react-navigation中重置路由?

问题描述 投票:1回答:1

男孩和女孩,我如何使用Stack Actions重置反应导航路线,并使用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方法中的所有订阅和异步任务。

javascript react-native react-navigation
1个回答
0
投票

你试图同时去2个不同的屏幕,屏幕createWorkerWorkers。做就是了: const resetAction = StackActions.reset({ index: 0, actions: [NavigationActions.navigate({ routeName: 'createWorker' })], }); this.props.navigation.dispatch(resetAction);

© www.soinside.com 2019 - 2024. All rights reserved.