Browse Source

Pass component passProps to componentDidAppear event (#5139)

Pass component passProps to componentDidAppear event
Yogev Ben David 5 years ago
parent
commit
c226a7d551

+ 2
- 1
docs/docs/events.md View File

@@ -45,7 +45,7 @@ This event can be observed globally as well:
45 45
 
46 46
 ```js
47 47
 // Subscribe
48
-const screenEventListener = Navigation.events().registerComponentDidAppearListener(({ componentId, componentName }) => {
48
+const screenEventListener = Navigation.events().registerComponentDidAppearListener(({ componentId, componentName, passProps }) => {
49 49
 
50 50
 });
51 51
 ...
@@ -56,6 +56,7 @@ screenEventListener.remove();
56 56
 |:--------------------:|:-----|
57 57
 |**componentId**| Id of the appearing component|
58 58
 |**componentName**|Registered name used when registering the component with `Navigation.registerComponent()`|
59
+|**passProps**| props passed to the component|
59 60
 
60 61
 ## componentDidDisappear
61 62
 Called each time this component disappears from screen (detached from the view heirarchy)

+ 1
- 1
lib/src/Navigation.ts View File

@@ -44,7 +44,7 @@ export class NavigationRoot {
44 44
     this.store = new Store();
45 45
     this.nativeEventsReceiver = new NativeEventsReceiver();
46 46
     this.uniqueIdProvider = new UniqueIdProvider();
47
-    this.componentEventsObserver = new ComponentEventsObserver(this.nativeEventsReceiver);
47
+    this.componentEventsObserver = new ComponentEventsObserver(this.nativeEventsReceiver, this.store);
48 48
     const appRegistryService = new AppRegistryService();
49 49
     this.componentRegistry = new ComponentRegistry(
50 50
       this.store,

+ 23
- 3
lib/src/events/ComponentEventsObserver.test.tsx View File

@@ -3,9 +3,12 @@ import * as renderer from 'react-test-renderer';
3 3
 import { ComponentEventsObserver } from './ComponentEventsObserver';
4 4
 import { NativeEventsReceiver } from '../adapters/NativeEventsReceiver.mock';
5 5
 import { EventSubscription } from '../interfaces/EventSubscription';
6
+import { Store } from '../components/Store';
7
+import { ComponentDidAppearEvent } from '../interfaces/ComponentEvents';
6 8
 
7 9
 describe('ComponentEventsObserver', () => {
8 10
   const mockEventsReceiver = new NativeEventsReceiver();
11
+  const mockStore = new Store();
9 12
   const didAppearFn = jest.fn();
10 13
   const didDisappearFn = jest.fn();
11 14
   const didMountFn = jest.fn();
@@ -84,8 +87,8 @@ describe('ComponentEventsObserver', () => {
84 87
       willUnmountFn();
85 88
     }
86 89
 
87
-    componentDidAppear() {
88
-      didAppearFn();
90
+    componentDidAppear(event: ComponentDidAppearEvent) {
91
+      didAppearFn(event);
89 92
     }
90 93
 
91 94
     componentDidDisappear() {
@@ -119,7 +122,7 @@ describe('ComponentEventsObserver', () => {
119 122
 
120 123
   beforeEach(() => {
121 124
     jest.clearAllMocks();
122
-    uut = new ComponentEventsObserver(mockEventsReceiver);
125
+    uut = new ComponentEventsObserver(mockEventsReceiver, mockStore);
123 126
   });
124 127
 
125 128
   it(`bindComponent expects a component with componentId`, () => {
@@ -192,6 +195,23 @@ describe('ComponentEventsObserver', () => {
192 195
     expect(willUnmountFn).toHaveBeenCalledTimes(1);
193 196
   });
194 197
 
198
+  it(`componentDidAppear should receive component props from store`, () => {
199
+    const event = {
200
+      componentId: 'myCompId',
201
+      passProps: {
202
+        propA: 'propA'
203
+      },
204
+      componentName: 'doesnt matter'
205
+    }
206
+    renderer.create(<BoundScreen componentId={event.componentId} />);
207
+    mockStore.setPropsForId(event.componentId, event.passProps)
208
+    expect(didAppearFn).not.toHaveBeenCalled();
209
+
210
+    uut.notifyComponentDidAppear({ componentId: 'myCompId', componentName: 'doesnt matter' });
211
+    expect(didAppearFn).toHaveBeenCalledTimes(1);
212
+    expect(didAppearFn).toHaveBeenCalledWith(event);
213
+  });
214
+
195 215
   it(`doesnt call other componentIds`, () => {
196 216
     renderer.create(<BoundScreen componentId={'myCompId'} />);
197 217
     uut.notifyComponentDidAppear({ componentId: 'other', componentName: 'doesnt matter' });

+ 6
- 1
lib/src/events/ComponentEventsObserver.ts View File

@@ -11,6 +11,7 @@ import {
11 11
   ModalDismissedEvent
12 12
 } from '../interfaces/ComponentEvents';
13 13
 import { NativeEventsReceiver } from '../adapters/NativeEventsReceiver';
14
+import { Store } from '../components/Store';
14 15
 
15 16
 type ReactComponentWithIndexing = React.Component<any> & Record<string, any>;
16 17
 
@@ -18,7 +19,10 @@ export class ComponentEventsObserver {
18 19
   private listeners: Record<string, Record<string, ReactComponentWithIndexing>> = {};
19 20
   private alreadyRegistered = false;
20 21
 
21
-  constructor(private readonly nativeEventsReceiver: NativeEventsReceiver) {
22
+  constructor(
23
+    private readonly nativeEventsReceiver: NativeEventsReceiver,
24
+    private readonly store: Store
25
+  ) {
22 26
     this.notifyComponentDidAppear = this.notifyComponentDidAppear.bind(this);
23 27
     this.notifyComponentDidDisappear = this.notifyComponentDidDisappear.bind(this);
24 28
     this.notifyNavigationButtonPressed = this.notifyNavigationButtonPressed.bind(this);
@@ -60,6 +64,7 @@ export class ComponentEventsObserver {
60 64
   }
61 65
 
62 66
   notifyComponentDidAppear(event: ComponentDidAppearEvent) {
67
+    event.passProps = this.store.getPropsForId(event.componentId);
63 68
     this.triggerOnAllListenersByComponentId(event, 'componentDidAppear');
64 69
   }
65 70
 

+ 1
- 0
lib/src/interfaces/ComponentEvents.ts View File

@@ -4,6 +4,7 @@ export interface ComponentEvent {
4 4
 
5 5
 export interface ComponentDidAppearEvent extends ComponentEvent {
6 6
   componentName: string;
7
+  passProps?: object
7 8
 }
8 9
 
9 10
 export interface ComponentDidDisappearEvent extends ComponentEvent {