1
- import { isEqual } from 'lodash' ;
2
1
import * as React from 'react' ;
3
2
import { getPlugin , getPluginContext , onStateChange } from 'ui-plugin' ;
4
- import { GetProps } from '../types' ;
3
+ import { PlugComponentType } from '../types' ;
5
4
6
5
type Props = {
7
6
pluginName : string ;
8
- component : React . ComponentType ;
7
+ component : PlugComponentType < any , any > ;
9
8
slotProps : object ;
10
- getProps : GetProps ;
11
9
children ?: React . ReactNode ;
12
10
} ;
13
11
14
- type State = {
15
- plugProps : object ;
16
- } ;
17
-
18
- export class PlugConnect extends React . Component < Props , State > {
19
- static getDerivedStateFromProps ( props : Props , state : State ) {
20
- const plugProps = getPlugProps ( props ) ;
21
-
22
- if ( plugPropsEqual ( plugProps , state . plugProps ) ) {
23
- return null ;
24
- }
25
-
26
- return { plugProps } ;
27
- }
28
-
29
- state = {
30
- plugProps : this . getPlugProps ( ) ,
31
- } ;
32
-
12
+ export class PlugConnect extends React . Component < Props > {
33
13
removeStateChangeHandler : null | ( ( ) => unknown ) = null ;
34
14
35
15
render ( ) {
36
16
const { component, children } = this . props ;
37
- const { plugProps } = this . state ;
17
+ const plugProps = this . getPlugProps ( ) ;
38
18
39
19
return React . createElement ( component , plugProps , children ) ;
40
20
}
@@ -57,31 +37,13 @@ export class PlugConnect extends React.Component<Props, State> {
57
37
// rendering is async, it takes a while for the Slot components to process
58
38
// plugin changes, so PlugConnect components might receive state changes
59
39
// for plugins that are no longer enabled.
60
- if ( ! getPlugin ( this . props . pluginName ) . enabled ) {
61
- return ;
62
- }
63
-
64
- const newProps = this . getPlugProps ( ) ;
65
-
66
- // NOTE: This can be optimized. We can avoid running plug.getProps when
67
- // relevant state hasn't changed.
68
- // TODO: How to avoid comparing annonymous dispatch-like functions that get
69
- // created on every getProps call?
70
- if ( ! plugPropsEqual ( newProps , this . state . plugProps ) ) {
71
- this . setState ( { plugProps : newProps } ) ;
40
+ if ( getPlugin ( this . props . pluginName ) . enabled ) {
41
+ this . forceUpdate ( ) ;
72
42
}
73
43
} ;
74
44
75
45
getPlugProps ( ) {
76
- return getPlugProps ( this . props ) ;
46
+ const { pluginName, slotProps = { } } = this . props ;
47
+ return { pluginContext : getPluginContext ( pluginName ) , slotProps } ;
77
48
}
78
49
}
79
-
80
- function getPlugProps ( { pluginName, slotProps, getProps } : Props ) {
81
- return getProps ( getPluginContext ( pluginName ) , slotProps ) ;
82
- }
83
-
84
- function plugPropsEqual ( plugProps1 : object , plugProps2 : object ) {
85
- // IDEA: Assume functions with same name are equal
86
- return isEqual ( plugProps1 , plugProps2 ) ;
87
- }
0 commit comments