1
votes

Boys and girls, how I can reset route in react-navigation with Stack Actions and use componentDidUpdate instead of componentDidMount.

Is it possible?

This is my code - 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();
  }

This is my code - 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);
  }

I don't want to use componentDidMount I want to use componentDidUpdate. Now stackaction work but in WorkersView I have that error:

Warning: Can't call setState (or forceUpdate) on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in the componentWillUnmount method.

1

1 Answers

0
votes

You are trying to go to 2 different screen at the same time, the screen createWorker and Workers. Just do:
const resetAction = StackActions.reset({ index: 0, actions: [NavigationActions.navigate({ routeName: 'createWorker' })], }); this.props.navigation.dispatch(resetAction);