簡體   English   中英

如何使用Stack Actions在反應導航中重置路線?

[英]How I can reset route in react-navigation with Stack Actions?

男孩和女孩,我該如何使用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方法中取消所有訂閱和異步任務。

您嘗試同時進入兩個不同的屏幕,屏幕createWorkerWorkers 做就是了:
const resetAction = StackActions.reset({ index: 0, actions: [NavigationActions.navigate({ routeName: 'createWorker' })], }); this.props.navigation.dispatch(resetAction);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM