Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F101367448
useCustomEffect.js
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Sun, Feb 9, 09:54
Size
1 KB
Mime Type
text/x-java
Expires
Tue, Feb 11, 09:54 (1 d, 23 h)
Engine
blob
Format
Raw Data
Handle
24145488
Attached To
rOACCT Open Access Compliance Check Tool (OACCT)
useCustomEffect.js
View Options
import
{
useRef
,
useEffect
,
useDebugValue
}
from
'react'
;
import
useMounted
from
'./useMounted'
;
function
useCustomEffect
(
effect
,
dependencies
,
isEqualOrOptions
)
{
var
isMounted
=
useMounted
();
var
_ref
=
typeof
isEqualOrOptions
===
'function'
?
{
isEqual
:
isEqualOrOptions
}
:
isEqualOrOptions
,
isEqual
=
_ref
.
isEqual
,
_ref$effectHook
=
_ref
.
effectHook
,
effectHook
=
_ref$effectHook
===
void
0
?
useEffect
:
_ref$effectHook
;
var
dependenciesRef
=
useRef
();
dependenciesRef
.
current
=
dependencies
;
var
cleanupRef
=
useRef
(
null
);
effectHook
(
function
()
{
// If the ref the is `null` it's either the first effect or the last effect
// ran and was cleared, meaning _this_ update should run, b/c the equality
// check failed on in the cleanup of the last effect.
if
(
cleanupRef
.
current
===
null
)
{
var
cleanup
=
effect
();
cleanupRef
.
current
=
function
()
{
if
(
isMounted
()
&&
isEqual
(
dependenciesRef
.
current
,
dependencies
))
{
return
;
}
cleanupRef
.
current
=
null
;
if
(
cleanup
)
cleanup
();
};
}
return
cleanupRef
.
current
;
});
useDebugValue
(
effect
);
}
export
default
useCustomEffect
;
Event Timeline
Log In to Comment