File tree 2 files changed +17
-12
lines changed
2 files changed +17
-12
lines changed Original file line number Diff line number Diff line change 1
1
{
2
2
"name" : " use-custom-element" ,
3
- "version" : " 1.0.3 " ,
3
+ "version" : " 1.0.4 " ,
4
4
"description" : " " ,
5
5
"main" : " lib/index.js" ,
6
6
"scripts" : {
Original file line number Diff line number Diff line change @@ -4,19 +4,24 @@ const useCustomElement = (props, customMapping = {}) => {
4
4
const ref = React . createRef ( ) ;
5
5
6
6
React . useLayoutEffect ( ( ) => {
7
- const fns = Object . keys ( props )
8
- . filter ( key => props [ key ] instanceof Function )
9
- . map ( key => ( {
10
- key : customMapping [ key ] || key ,
11
- fn : customEvent =>
12
- props [ key ] ( customEvent . detail , customEvent ) ,
13
- } ) ) ;
14
-
15
- fns . forEach ( ( { key, fn } ) =>
16
- ref . current . addEventListener ( key , fn ) ,
17
- ) ;
7
+ let fns ;
8
+
9
+ if ( ref . current ) {
10
+ fns = Object . keys ( props )
11
+ . filter ( key => props [ key ] instanceof Function )
12
+ . map ( key => ( {
13
+ key : customMapping [ key ] || key ,
14
+ fn : customEvent =>
15
+ props [ key ] ( customEvent . detail , customEvent ) ,
16
+ } ) ) ;
17
+
18
+ fns . forEach ( ( { key, fn } ) =>
19
+ ref . current . addEventListener ( key , fn ) ,
20
+ ) ;
21
+ }
18
22
19
23
return ( ) =>
24
+ ref . current &&
20
25
fns . forEach ( ( { key, fn } ) =>
21
26
ref . current . removeEventListener ( key , fn ) ,
22
27
) ;
You can’t perform that action at this time.
0 commit comments