@@ -38,23 +38,27 @@ export default class Canvas extends Component {
38
38
}
39
39
40
40
renderSource ( ) {
41
- const div = this . refs . source ;
41
+ if ( this . shouldUpdate ) {
42
+ const div = this . refs . source ;
42
43
43
- if ( div instanceof HTMLElement ) {
44
- const args = [ 'context' , 'React' ] , argv = [ this . props . context , React ] ;
44
+ if ( div instanceof HTMLElement ) {
45
+ const args = [ 'context' , 'React' ] , argv = [ this . props . context , React ] ;
45
46
46
- const Element = require ( '../../src' ) ;
47
+ const Element = require ( '../../src' ) ;
47
48
48
- for ( const key in Element ) {
49
- args . push ( key ) ;
50
- argv . push ( Element [ key ] ) ;
51
- }
49
+ for ( const key in Element ) {
50
+ args . push ( key ) ;
51
+ argv . push ( Element [ key ] ) ;
52
+ }
52
53
53
- args . push ( this . component ) ;
54
+ args . push ( this . component ) ;
54
55
55
- ReactDOM . unmountComponentAtNode ( div ) ;
56
- ReactDOM . render ( new Function ( ...args ) . apply ( null , argv ) , div ) ;
56
+ ReactDOM . unmountComponentAtNode ( div ) ;
57
+ ReactDOM . render ( new Function ( ...args ) . apply ( null , argv ) , div ) ;
58
+ }
57
59
}
60
+
61
+ delete this . shouldUpdate ;
58
62
}
59
63
60
64
render ( ) {
@@ -70,10 +74,13 @@ export default class Canvas extends Component {
70
74
code = `<div>${ source [ 2 ] } </div>`
71
75
}
72
76
73
- this . component = transform ( code . replace ( / t h i s / g, 'context' ) , {
77
+ const component = transform ( code . replace ( / t h i s / g, 'context' ) , {
74
78
presets : [ 'es2015' , 'react' ]
75
79
} ) . code . replace ( / R e a c t .c r e a t e E l e m e n t / , 'return React.createElement' ) ;
76
80
81
+ this . shouldUpdate = component != this . component || this . component === undefined ;
82
+ this . component = component ;
83
+
77
84
return (
78
85
< div className = { `demo-block demo-box demo-${ name } ` } >
79
86
< div className = "source" ref = "source" > </ div >
0 commit comments