Skip to content

Commit c0a5f6a

Browse files
committed
fix: fixed navigation.setParams not working
1 parent 9c4136d commit c0a5f6a

File tree

4 files changed

+29
-46
lines changed

4 files changed

+29
-46
lines changed

src/SharedElementRendererData.tsx

Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import {
44
SharedElementsConfig,
55
SharedElementAnimatedValue,
66
SharedElementTransitionProps,
7-
NavigationProp,
87
} from './types';
98
import { normalizeSharedElementsConfig } from './utils';
109

@@ -13,35 +12,27 @@ export type SharedElementRendererUpdateHandler = () => any;
1312
export interface ISharedElementRendererData {
1413
startTransition(animValue: SharedElementAnimatedValue): void;
1514
endTransition(): void;
16-
willActivateScene(
17-
sceneData: SharedElementSceneData,
18-
navigation: NavigationProp
19-
): void;
20-
didActivateScene(
21-
sceneData: SharedElementSceneData,
22-
navigation: NavigationProp
23-
): void;
15+
willActivateScene(sceneData: SharedElementSceneData): void;
16+
didActivateScene(sceneData: SharedElementSceneData): void;
2417
}
2518

2619
function getSharedElements(
2720
sceneData: SharedElementSceneData,
28-
navigation: NavigationProp,
29-
otherNavigation: NavigationProp,
21+
otherSceneData: SharedElementSceneData,
3022
show: boolean
3123
): SharedElementsConfig | null {
3224
const { sharedElements } = sceneData.Component;
3325
if (!sharedElements) return null;
3426
// TODO push/pop distinction?
3527
return normalizeSharedElementsConfig(
36-
sharedElements(navigation, otherNavigation, show)
28+
sharedElements(sceneData.navigation, otherSceneData.navigation, show)
3729
);
3830
}
3931

4032
export default class SharedElementRendererData
4133
implements ISharedElementRendererData {
4234
private sceneData: SharedElementSceneData | null = null;
4335
private prevSceneData: SharedElementSceneData | null = null;
44-
private prevNavigation: NavigationProp | null = null;
4536
private updateSubscribers = new Set<SharedElementRendererUpdateHandler>();
4637
private sceneSubscription: SharedElementEventSubscription | null = null;
4738
private sharedElements: SharedElementsConfig = [];
@@ -55,25 +46,17 @@ export default class SharedElementRendererData
5546
// Nothing to do
5647
}
5748

58-
willActivateScene(
59-
sceneData: SharedElementSceneData,
60-
navigation: NavigationProp
61-
): void {
49+
willActivateScene(sceneData: SharedElementSceneData): void {
6250
/*console.log(
6351
'SharedElementRendererData.willActivateScene: ',
6452
sceneData.name,
6553
', previous: ',
6654
this.prevSceneData ? this.prevSceneData.name : ''
6755
);*/
68-
if (!this.prevSceneData || !this.prevNavigation) return;
56+
if (!this.prevSceneData) return;
6957
const sharedElements =
70-
getSharedElements(sceneData, navigation, this.prevNavigation, true) ||
71-
getSharedElements(
72-
this.prevSceneData,
73-
this.prevNavigation,
74-
navigation,
75-
false
76-
);
58+
getSharedElements(sceneData, this.prevSceneData, true) ||
59+
getSharedElements(this.prevSceneData, sceneData, false);
7760
if (sharedElements && sharedElements.length) {
7861
// console.log('sharedElements: ', sharedElements, sceneData);
7962
this.sceneData = sceneData;
@@ -86,17 +69,13 @@ export default class SharedElementRendererData
8669
}
8770
}
8871

89-
didActivateScene(
90-
sceneData: SharedElementSceneData,
91-
navigation: NavigationProp
92-
): void {
72+
didActivateScene(sceneData: SharedElementSceneData): void {
9373
//console.log('SharedElementRendererData.didActivateScene: ', sceneData.name);
9474
if (this.sceneSubscription) {
9575
this.sceneSubscription.remove();
9676
this.sceneSubscription = null;
9777
}
9878
this.prevSceneData = sceneData;
99-
this.prevNavigation = navigation;
10079
if (this.sceneData) {
10180
this.sceneData = null;
10281
if (this.sharedElements.length) {

src/SharedElementRendererProxy.tsx

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import SharedElementRendererData, {
22
ISharedElementRendererData,
33
} from './SharedElementRendererData';
4-
import { SharedElementAnimatedValue, NavigationProp } from './types';
4+
import { SharedElementAnimatedValue } from './types';
55
import SharedElementSceneData from './SharedElementSceneData';
66

77
export class SharedElementRendererProxy implements ISharedElementRendererData {
@@ -27,30 +27,24 @@ export class SharedElementRendererProxy implements ISharedElementRendererData {
2727
return this.data.endTransition();
2828
}
2929

30-
willActivateScene(
31-
sceneData: SharedElementSceneData,
32-
navigation: NavigationProp
33-
) {
30+
willActivateScene(sceneData: SharedElementSceneData) {
3431
if (!this.data) {
3532
console.warn(
3633
'SharedElementRendererProxy.willActivateScene called before Proxy was initialized'
3734
);
3835
return;
3936
}
40-
return this.data.willActivateScene(sceneData, navigation);
37+
return this.data.willActivateScene(sceneData);
4138
}
4239

43-
didActivateScene(
44-
sceneData: SharedElementSceneData,
45-
navigation: NavigationProp
46-
) {
40+
didActivateScene(sceneData: SharedElementSceneData) {
4741
if (!this.data) {
4842
console.warn(
4943
'SharedElementRendererProxy.didActivateScene called before Proxy was initialized'
5044
);
5145
return;
5246
}
53-
return this.data.didActivateScene(sceneData, navigation);
47+
return this.data.didActivateScene(sceneData);
5448
}
5549

5650
get source(): SharedElementRendererData | null {

src/SharedElementSceneData.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
SharedElementNode,
33
SharedElementEventSubscription,
44
SharedElementSceneComponent,
5+
NavigationProp,
56
} from './types';
67

78
export type SharedElementSceneUpdateHandlerEventType =
@@ -23,9 +24,14 @@ export default class SharedElementSceneData {
2324
} = {};
2425
public readonly Component: SharedElementSceneComponent;
2526
public readonly name: string;
27+
public navigation: NavigationProp;
2628

27-
constructor(Component: SharedElementSceneComponent) {
29+
constructor(
30+
Component: SharedElementSceneComponent,
31+
navigation: NavigationProp
32+
) {
2833
this.Component = Component;
34+
this.navigation = navigation;
2935
this.name =
3036
Component.displayName ||
3137
Component.name ||

src/createSharedElementScene.tsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ function createSharedElementScene(
4242
[key: string]: SharedElementEventSubscription;
4343
} = {};
4444
private sceneData: SharedElementSceneData = new SharedElementSceneData(
45-
Component
45+
Component,
46+
this.props.navigation
4647
);
4748

4849
componentDidMount() {
@@ -74,21 +75,24 @@ function createSharedElementScene(
7475
);
7576
}
7677

78+
componentDidUpdate() {
79+
this.sceneData.navigation = this.props.navigation;
80+
}
81+
7782
private onSetRef = (ref: any) => {
7883
this.sceneData.setAncestor(nodeFromRef(ref));
7984
};
8085

8186
private onWillFocus = () => {
82-
const { navigation } = this.props;
83-
rendererData.willActivateScene(this.sceneData, navigation);
87+
rendererData.willActivateScene(this.sceneData);
8488
};
8589

8690
private onDidFocus = () => {
8791
const { navigation } = this.props;
8892
const activeRoute = getActiveRouteState(navigation.state);
8993
if (navigation.state.routeName === activeRoute.routeName) {
9094
// console.log('onDidFocus: ', this.sceneData.name, navigation);
91-
rendererData.didActivateScene(this.sceneData, navigation);
95+
rendererData.didActivateScene(this.sceneData);
9296
}
9397
};
9498
}

0 commit comments

Comments
 (0)