question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Navigator] Animated replace

See original GitHub issue

I often find myself wanting to “replace” a route i.e not adding to the history yet have the route change animate.

In iOS I would do something like this.

[navigationController popToRootViewControllerAnimated:NO];
[navigationController pushViewController:someOtherViewController animated:YES];

Maybe this is related to issue #1953 I think I would prefer an api where we could do

replace(route, animated);
push(route, animated);

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:4
  • Comments:27 (10 by maintainers)

github_iconTop GitHub Comments

24reactions
zhaotaicommented, Jun 3, 2016

I have tried all of these solutions above but all didn’t work. So I had to check the Navigator source code while coded on my own. Finally after solving some problems such as flashing and animating, not working at all, replacing without animation though I have written the code, I got the solution.

These are my code below. Most of them are copied from Navigator source code. What I did is just combine them in a right way. Um…of course, they still can be optimized. And they too much hard to understand. Trust me, I won’t understand after tonight. But it works. It’s enough. BTW, my RN is 0.27.

Navigator.prototype.replaceWithAnimation = function (route) {
  const activeLength = this.state.presentedIndex + 1;
  const activeStack = this.state.routeStack.slice(0, activeLength);
  const activeAnimationConfigStack = this.state.sceneConfigStack.slice(0, activeLength);
  const nextStack = activeStack.concat([route]);
  const destIndex = nextStack.length - 1;
  const nextSceneConfig = this.props.configureScene(route, nextStack);
  const nextAnimationConfigStack = activeAnimationConfigStack.concat([nextSceneConfig]);

  const replacedStack = activeStack.slice(0, activeLength - 1).concat([route]);
  this._emitWillFocus(nextStack[destIndex]);
  this.setState({
    routeStack: nextStack,
    sceneConfigStack: nextAnimationConfigStack,
  }, () => {
    this._enableScene(destIndex);
    this._transitionTo(destIndex, nextSceneConfig.defaultTransitionVelocity, null, () => {
      this.immediatelyResetRouteStack(replacedStack);
    });
  });
};

After defining that in prototype, you can use it anywhere you want. Just like navigator.replaceWithAnimation(route);

2reactions
miracle2kcommented, Oct 10, 2015

Inspired by the suggestion from @grabbou, I am now using this workaround:

function betterNavigator(navigator) {
  const navigation = {
    pop: () => {
      debugger;
      const routes = navigator.getCurrentRoutes();

      // Find the next route without replace mode
      var target = null;
      for (let i=routes.length-2; i>=0; i--) {
        if (!routes[i].replace) {
          target = routes[i];
          break;
        }
      }

      if (target)
        navigator.popToRoute(target);
      else
        navigator.pop();
    },
    popToTop: navigator.popToTop,
    popToRoute: navigator.popToRoute,
    popBack: (index) => {
      const routes = navigator.getCurrentRoutes();
      navigator.popToRoute(routes[routes.length - index - 1]);
    },
    replaceAtIndex: navigator.replaceAtIndex,
    push: navigator.push,
    replace: (target) => {
      debugger;
      const routes = navigator.getCurrentRoutes();
      routes[routes.length-1].replace = true;
      navigator.push(target);
    },
    getCurrentRoutes: navigator.getCurrentRoutes
  };
  return navigation;
}

Note the overwrites of replace() and pop(). pop will skip all routes that have been replaced.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to change navigation animation using Flutter
You can achieve this by using CupertinoPageRoute. Please check the below code. import 'package:flutter/material.dart'; import 'package:flutter/ ...
Read more >
Animate a page route transition - Flutter documentation
To help, PageRouteBuilder provides an Animation object. This Animation can be used with Tween and Curve objects to customize the transition animation.
Read more >
navigation.replace animation with 2.0.x · Issue #7031 - GitHub
Resolved with the following work around: Pass a route param animate: false when going to this screen with a .replace call. Then on...
Read more >
Stack Navigator | React Navigation
If you specify a custom header, React Navigation will change it to screen automatically so that the header animated along with the screen...
Read more >
Custom Animated Transition in React Navigation with Stack ...
In this video, we create Custom Animated Transition in React Navigation with Stack Navigator (aka shared transition).
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found