Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F99418856
useUpdateEffect.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
Fri, Jan 24, 09:37
Size
815 B
Mime Type
text/x-java
Expires
Sun, Jan 26, 09:37 (1 d, 23 h)
Engine
blob
Format
Raw Data
Handle
23788361
Attached To
rOACCT Open Access Compliance Check Tool (OACCT)
useUpdateEffect.js
View Options
import
{
useEffect
,
useRef
}
from
'react'
;
/**
* Runs an effect only when the dependencies have changed, skipping the
* initial "on mount" run. Caution, if the dependency list never changes,
* the effect is **never run**
*
* ```ts
* const ref = useRef<HTMLInput>(null);
*
* // focuses an element only if the focus changes, and not on mount
* useUpdateEffect(() => {
* const element = ref.current?.children[focusedIdx] as HTMLElement
*
* element?.focus()
*
* }, [focusedIndex])
* ```
* @param effect An effect to run on mount
*
* @category effects
*/
function
useUpdateEffect
(
fn
,
deps
)
{
var
isFirst
=
useRef
(
true
);
useEffect
(
function
()
{
if
(
isFirst
.
current
)
{
isFirst
.
current
=
false
;
return
;
}
return
fn
();
},
deps
);
}
export
default
useUpdateEffect
;
Event Timeline
Log In to Comment