
Security News
Axios Maintainer Confirms Social Engineering Attack Behind npm Compromise
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.
use-safe-async-mount
Advanced tools
use-safe-async-mount
The Asynchronous Functional Component Mounter
This is a useEffect hook with zero dependencies:
useEffect(() => {
// Code that runs "on mount" (unfortunately)
}, [])
It is the most incorrectly [and dangerously] used part of the react functional component lifecycle.
Whenever using this hook (with the empty dependency array), you should ask yourself two questions:
Am I using this to just initialize a variable based on some synchronously computed value?
useEffect(() => { setValue(computeMyValue()) }, [])Am I using this to just conditionally initialize a state variable based on props?
useEffect(() => { setValue(someProp ? "a" : "b") }, [])If either of these situations describes you, there's a high chance you should be just be computing the value in-line or using useMemo. This saves you from having to deal with the initial render when your value (useState, useRef, var, etc.) is undefined.
Here's some situations that do fit in the empty useEffect hook:
When a component's state depends on a value gathered from an async function, the common solution is to manually invoke it directly from the empty useEffect hook:
function ExampleComponent() {
const [stateOne, setStateOne] = useState()
useEffect(() => {
someAsyncFunction().then(res => setStateOne(res))
// OR
const run = async () => {
const res = await someAsyncFunction()
setStateOne(res)
}
run();
}, [])
return (
<>
<h1>My Example Component</h1>
{stateOne && <p>{stateOne}</p>}
</>
)
}
There's three negative implications to this solution:
!'s everywhere.npm i use-safe-async-mount
use-safe-async-mount solves these problems by acting as a true hook-based, type-safe componentWillMount implementation.
import useSafeAsyncMount from 'use-safe-async-mount';
function ExampleComponent() {
const { SafeRender } = useSafeAsyncMount(async isActive => {
const res = await someAsyncFunction()
if (isActive()) {
// ^ This avoids setting component state after unmount
// These values are defined and type-safe in the `SafeRender` component
return {
stateOne: "Some value my component depends on",
stateTwo: res
}
}
})
return (
<>
<h1>My Example Component</h1>
<SafeRender>
{({ stateOne, stateTwo }) => (
<p>{stateOne}</p>
)}
</SafeRender>
</>
)
}
Here's an interactive example and the associated source code.
FAQs
<!-- markdownlint-disable MD033 MD041 -->
We found that use-safe-async-mount demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.

Security News
The Axios compromise shows how time-dependent dependency resolution makes exposure harder to detect and contain.