File tree 2 files changed +54
-2
lines changed
2 files changed +54
-2
lines changed Original file line number Diff line number Diff line change @@ -40,13 +40,13 @@ function mergeComponents(
40
40
if ( key . startsWith ( PREFIX ) ) return
41
41
const nextAttr = nextInstance [ key ]
42
42
const prevAttr = proxyInstance [ key ]
43
- if ( prevAttr && nextAttr ) {
43
+ if ( nextAttr ) {
44
44
if ( isNativeFunction ( nextAttr ) || isNativeFunction ( prevAttr ) ) {
45
45
// this is bound method
46
46
const isSameArity = nextAttr . length === prevAttr . length
47
47
const existsInPrototype =
48
48
ownKeys . indexOf ( key ) >= 0 || ProxyComponent . prototype [ key ]
49
- if ( isSameArity && existsInPrototype ) {
49
+ if ( ( isSameArity || ! prevAttr ) && existsInPrototype ) {
50
50
if ( hasRegenerate ) {
51
51
injectedCode [
52
52
key
@@ -109,6 +109,10 @@ function mergeComponents(
109
109
} else {
110
110
// key was skipped
111
111
}
112
+ } else {
113
+ // key does not exists anymore
114
+ // we could not delete it, yet #840
115
+ // injectedCode[key] = null;
112
116
}
113
117
} )
114
118
} catch ( e ) {
Original file line number Diff line number Diff line change @@ -105,4 +105,52 @@ describe('lifecycle method', () => {
105
105
wrapper . instance ( ) . forceUpdate ( )
106
106
expect ( wrapper . text ( ) ) . toContain ( 'PATCHED + !15' )
107
107
} )
108
+
109
+ it ( 'handle dynamic method creation' , ( ) => {
110
+ class App1 extends Component {
111
+ method1 = ( ) => 41 + this . var1
112
+
113
+ var1 = 1
114
+
115
+ render ( ) {
116
+ return < div > { this . method1 ( ) } </ div >
117
+ }
118
+ }
119
+
120
+ class App2 extends Component {
121
+ method2 = ( ) => 22 + this . var2
122
+
123
+ var2 = 2
124
+
125
+ /* eslint-disable */
126
+ __reactstandin__regenerateByEval ( key , code ) {
127
+ this [ key ] = eval ( code )
128
+ }
129
+ /* eslint-enable */
130
+
131
+ render ( ) {
132
+ return (
133
+ < div >
134
+ { this . method1 ( ) } + { this . method2 ( ) }
135
+ </ div >
136
+ )
137
+ }
138
+ }
139
+
140
+ const proxy = createProxy ( App1 )
141
+ const Proxy = proxy . get ( )
142
+
143
+ const wrapper = mount ( < Controller Proxy = { Proxy } /> )
144
+
145
+ expect ( wrapper . text ( ) ) . toContain ( '42' )
146
+
147
+ proxy . update ( App2 )
148
+ wrapper . instance ( ) . forceUpdate ( )
149
+
150
+ // first render before hot render
151
+ expect ( wrapper . text ( ) ) . toContain ( '42' )
152
+ wrapper . instance ( ) . forceUpdate ( )
153
+ // both methods expected to be present
154
+ expect ( wrapper . text ( ) ) . toContain ( '42 + 24' )
155
+ } )
108
156
} )
You can’t perform that action at this time.
0 commit comments