Security News
Opengrep Emerges as Open Source Alternative Amid Semgrep Licensing Controversy
Opengrep forks Semgrep to preserve open source SAST in response to controversial licensing changes.
react-usemountedstate
Advanced tools
Use state in asynchronous unmountable components without getting errors
A React.js hook to use the official useState hook on super-duper complicated components with a lot asynchronous setStates
that might be executed, when a component might be already unmounted.
Install with npm:
# via npm
npm install react-usemountedstate
# via yarn
yarn add react-usemountedstate
The useMountedState
function takes no arguments, it creates the slightly special useState
hook.
It returns a function, that is supposed to be used instead of the original useState
.
Otherwise the useState usage remains the same.
import React, {useState as useReactState} from 'react';
import useMountedState from 'react-usemountedstate';
// use in Component
function App(props) {
const [showComp, setShowComp] = useReactState(true);
React.useEffect(() => {
setTimeout(() => {
setShowComp(false);
}, 3000);
}, []);
return (<>
{showComp && <Comp />}
</>);
};
function Comp(props) {
const useState = useMountedState(); // Create useState hook -> instead of the original React.useState
const [somestate, setSomestate] = useState('initial value'); // Use the new hook as usual! Just like React.useState
React.useEffect(() => {
setTimeout(() => {
setSomestate('Other value!'); // That should not be done!!! It should be cleaned up properly!
}, 6000);
}, []);
return (<div>
{somestate}
</div>);
};
MIT Copyright (c) 2020 Nikita 'donnikitos' Nitichevski.
FAQs
Use state in asynchronous unmountable components without getting errors
The npm package react-usemountedstate receives a total of 15 weekly downloads. As such, react-usemountedstate popularity was classified as not popular.
We found that react-usemountedstate 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
Opengrep forks Semgrep to preserve open source SAST in response to controversial licensing changes.
Security News
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
Security News
cURL and Go security teams are publicly rejecting CVSS as flawed for assessing vulnerabilities and are calling for more accurate, context-aware approaches.