Coder Social home page Coder Social logo

Comments (2)

StasD avatar StasD commented on May 1, 2024

I figured it out how to implement the functionality which I need using the current version of "react navigation". So, nevermind. :)

Here is what I did:

NavigationService.js:

import { NavigationActions, StackActions } from 'react-navigation';

let _navigator = null;
let _defaultGetStateForAction = null;

let _getStateForAction = (action, state) => {
  if (state && action.type === NavigationActions.BACK) {
    let _params = _getCurrentRoute(state).params;
    if (_params && _params.onBackAction && _params.onBackAction()) {
      return null; // action has been taken care of already
    }
  }

  return _defaultGetStateForAction(action, state);
};

function _getCurrentRoute(state) {
  while (state.routes) {
    state = state.routes[state.index];
  }

  return state;
}

function back() {
  _navigator.dispatch(
    NavigationActions.back({
      key: _getCurrentRoute(_navigator.state.nav).key,
    })
  );
}

function navigate(routeName, params = {}) {
  _navigator.dispatch(
    StackActions.push({
      routeName,
      params,
    })
  );
}

function navigateHome() {
  _navigator.dispatch(
    StackActions.popToTop()
  );
}

function setTopLevelNavigator(navigatorRef) {
  if (navigatorRef && _navigator === null) {
    _navigator = navigatorRef;
    let router = _navigator._navigation.router;
    _defaultGetStateForAction = router.getStateForAction;
    router.getStateForAction = _getStateForAction;
  }
}

export default {
  back,
  navigate,
  navigateHome,
  setTopLevelNavigator,
};

DetailsScreen.js:

import React, { Component } from 'react';
import { Button, View, Text } from 'react-native';

import nav from '../utils/NavigationService';

export default class DetailsScreen extends Component {
  static navigationOptions = {
    title: 'Details',
  };

  state = {
    itemId: null
  };

  stateHistory = [];

  _onBackAction = () => {
    if (this.stateHistory.length === 0)
      return false;

    let _newState = this.stateHistory.pop();
    this.setState(_newState);

    return true;
  }

  _updateState = (_newState) => {
    this.stateHistory.push({...this.state});
    this.setState(_newState);
  }

  componentDidMount() {
    const { navigation } = this.props;

    navigation.setParams({
      onBackAction: this._onBackAction
    });

    const itemId = navigation.getParam('itemId', 0);

    this.setState({
      itemId
    });
  }

  render() {
    return (
      <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
        <Text>Details Screen</Text>
        <Text>itemId: {this.state.itemId}</Text>
        <Button
          title="Go to Home"
          onPress={() => nav.navigateHome()}
        />
        <Button
          title="Go to Details (again...)"
          onPress={() => nav.navigate('Details', {
              itemId: Math.floor(Math.random() * 100),
            })}
        />
        <Button
          title="Update itemId"
          onPress={() => this._updateState({
              itemId: Math.floor(Math.random() * 100),
            })}
        />
        <Button
          title="Go to Another"
          onPress={() => nav.navigate('Another')}
        />
        <Button
          title="Go back"
          onPress={() => nav.back()}
        />
      </View>
    );
  }
}

from rfcs.

satya164 avatar satya164 commented on May 1, 2024

Closing since this repo is for proposals, not questions.

from rfcs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.