File tree Expand file tree Collapse file tree 2 files changed +28
-1
lines changed Expand file tree Collapse file tree 2 files changed +28
-1
lines changed Original file line number Diff line number Diff line change 1
1
import { h , options } from 'preact' ;
2
2
import { useState , useRef } from 'preact/hooks' ;
3
3
4
+ const oldDiff = options . __b ;
5
+ options . __b = ( vnode ) => {
6
+ if ( vnode . type && vnode . type . _forwarded && vnode . ref ) {
7
+ vnode . props . ref = vnode . ref ;
8
+ vnode . ref = null ;
9
+ }
10
+ if ( oldDiff ) oldDiff ( vnode ) ;
11
+ } ;
12
+
4
13
export default function lazy ( load ) {
5
14
let p , c ;
6
15
@@ -21,6 +30,7 @@ export default function lazy(load) {
21
30
return p ;
22
31
}
23
32
33
+ LazyComponent . _forwarded = true ;
24
34
return LazyComponent ;
25
35
}
26
36
Original file line number Diff line number Diff line change @@ -4,7 +4,7 @@ import * as sinon from 'sinon';
4
4
import sinonChai from 'sinon-chai' ;
5
5
6
6
import { LocationProvider , Router } from '../src/router.js' ;
7
- import lazy from '../src/lazy.js' ;
7
+ import lazy , { ErrorBoundary } from '../src/lazy.js' ;
8
8
9
9
const expect = chai . expect ;
10
10
chai . use ( sinonChai ) ;
@@ -42,4 +42,21 @@ describe('lazy', () => {
42
42
await B . preload ( ) ;
43
43
expect ( loadB ) . to . have . been . calledOnce ;
44
44
} ) ;
45
+
46
+ it ( 'should forward refs' , async ( ) => {
47
+ const A = ( ) => < h1 > A</ h1 > ;
48
+ const LazyA = lazy ( ( ) => Promise . resolve ( A ) ) ;
49
+
50
+ const ref = { } ;
51
+
52
+ render (
53
+ < ErrorBoundary >
54
+ < LazyA ref = { ref } />
55
+ </ ErrorBoundary > ,
56
+ scratch
57
+ ) ;
58
+ await new Promise ( r => setTimeout ( r , 1 ) )
59
+
60
+ expect ( ref . current . constructor ) . to . equal ( A ) ;
61
+ } ) ;
45
62
} ) ;
You can’t perform that action at this time.
0 commit comments