diff --git a/Libraries/NavigationExperimental/Reducer/NavigationScenesReducer.js b/Libraries/NavigationExperimental/Reducer/NavigationScenesReducer.js index a858a11e9..3e08b8727 100644 --- a/Libraries/NavigationExperimental/Reducer/NavigationScenesReducer.js +++ b/Libraries/NavigationExperimental/Reducer/NavigationScenesReducer.js @@ -12,6 +12,7 @@ 'use strict'; const invariant = require('fbjs/lib/invariant'); +const shallowEqual = require('fbjs/lib/shallowEqual'); import type { NavigationRoute, @@ -55,6 +56,9 @@ function compareScenes( ); } +/** + * Whether two routes are the same. + */ function areScenesShallowEqual( one: NavigationScene, two: NavigationScene, @@ -63,10 +67,28 @@ function areScenesShallowEqual( one.key === two.key && one.index === two.index && one.isStale === two.isStale && - one.route.key === two.route.key + areRoutesShallowEqual(one.route, two.route) ); } +/** + * Whether two routes are the same. + */ +function areRoutesShallowEqual( + one: ?NavigationRoute, + two: ?NavigationRoute, +): boolean { + if (!one || !two) { + return one === two; + } + + if (one.key !== two.key) { + return false; + } + + return shallowEqual(one, two); +} + function NavigationScenesReducer( scenes: Array, nextState: NavigationState, diff --git a/Libraries/NavigationExperimental/Reducer/__tests__/NavigationScenesReducer-test.js b/Libraries/NavigationExperimental/Reducer/__tests__/NavigationScenesReducer-test.js index 66373efe6..413adff62 100644 --- a/Libraries/NavigationExperimental/Reducer/__tests__/NavigationScenesReducer-test.js +++ b/Libraries/NavigationExperimental/Reducer/__tests__/NavigationScenesReducer-test.js @@ -113,7 +113,7 @@ describe('NavigationScenesReducer', () => { expect(scenes1).toBe(scenes2); }); - it('gets different scenes', () => { + it('gets different scenes when keys are different', () => { const state1 = { index: 0, routes: [{key: '1'}, {key: '2'}], @@ -129,6 +129,23 @@ describe('NavigationScenesReducer', () => { expect(scenes1).not.toBe(scenes2); }); + it('gets different scenes when routes are different', () => { + const state1 = { + index: 0, + routes: [{key: '1', x: 1}, {key: '2', x: 2}], + }; + + const state2 = { + index: 0, + routes: [{key: '1', x: 3}, {key: '2', x: 4}], + }; + + const scenes1 = NavigationScenesReducer([], state1, null); + const scenes2 = NavigationScenesReducer(scenes1, state2, state1); + expect(scenes1).not.toBe(scenes2); + }); + + it('pops scenes', () => { // Transition from ['1', '2', '3'] to ['1', '2']. const scenes = testTransition([