componentWillUnmount functional component for asynchronous requests
function usePromise2(url) {
const isMounted = React.useRef(true)
const [value, setState] = useState(null);
useEffect(() => {
return () => {
isMounted.current = false;
};
}, []);
useEffect(() => {
request.get(url)
.then(result => {
if (isMounted.current) {
setState(result);
}
});
}, []);
return value;
}
// or extract it to custom hook:
function useIsMounted() {
const isMounted = React.useRef(true)
useEffect(() => {
return () => {
isMounted.current = false;
};
}, []);
return isMounted; // returning "isMounted.current" wouldn't work because we would return unmutable primitive
}
Also in JavaScript:
- asp net core use newtonsoft json
- jquery validator no space
- Prevent Double Submit with JavaScript
- nodejs format text
- discord.js clear code
- react native map repeate
- express payloadtoolarge request entity too large bodyParser raw-body hydra-express
- js regex password
- swift convert array to json
- arrow function in javascript
- reqeuest body in hapijs
- how to pass a value to a react funtion without immediately firing it
- check the doc name in javascript
- Error: It looks like you are passing several store enhancers to createStore(). This is not supported. Instead, compose them together to a single function.
- click anchor tag using jquery
- javascript substring
- how to set html label value in jquery
- javascript how to check if image exists
- change the value in checkbox by button react
- jquery select element with data
- angular 7 folder structure best practices
- how to freeze js object
- react lifecycle hooks
- check row empty array javascript