File tree Expand file tree Collapse file tree 1 file changed +8
-7
lines changed Expand file tree Collapse file tree 1 file changed +8
-7
lines changed Original file line number Diff line number Diff line change 1
1
class Tree {
2
- constructor ( component ) {
2
+ constructor ( component , useStateInstead = false ) {
3
3
// special case when component is root
4
4
// give it a special state = 'root'
5
5
// a setState function that just calls the callback instantly
6
- this . component = ( component === 'root' ) ? { state : 'root' , setState : ( partial , callback ) => callback ( ) } : component ;
6
+ if ( ! useStateInstead ) {
7
+ this . component = ( component === 'root' ) ? { state : 'root' , setState : ( partial , callback ) => callback ( ) } : component ;
8
+ } else {
9
+ this . state = component ;
10
+ }
7
11
this . children = [ ] ;
8
12
}
9
13
@@ -14,12 +18,9 @@ class Tree {
14
18
}
15
19
16
20
// deep copies only the state of each component and creates a new tree
17
- getCopy ( copy = new Tree ( null ) ) {
18
- const { state } = this . component ;
19
- if ( ! copy . component ) copy . component = { state } ;
20
-
21
+ getCopy ( copy = new Tree ( 'root' , true ) ) {
21
22
// copy state of children
22
- copy . children = this . children . map ( child => new Tree ( { state : child . component . state } ) ) ;
23
+ copy . children = this . children . map ( child => new Tree ( child . component . state , true ) ) ;
23
24
24
25
// copy children's children recursively
25
26
this . children . forEach ( ( child , i ) => child . getCopy ( copy . children [ i ] ) ) ;
You can’t perform that action at this time.
0 commit comments