react-remote-resource
Advanced tools
Comparing version 0.1.1 to 0.1.2
@@ -267,2 +267,9 @@ 'use strict'; | ||
var mounted = React.useRef(true); | ||
React.useEffect(function () { | ||
return function () { | ||
mounted.current = false; | ||
}; | ||
}, []); | ||
if (task) { | ||
@@ -274,5 +281,9 @@ throw task; | ||
return setTask(fn().then(function () { | ||
setTask(null); | ||
if (mounted.current) { | ||
setTask(null); | ||
} | ||
}).catch(function () { | ||
setTask(null); | ||
if (mounted.current) { | ||
setTask(null); | ||
} | ||
})); | ||
@@ -279,0 +290,0 @@ }; |
{ | ||
"name": "react-remote-resource", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "Intuitive remote data management in React", | ||
@@ -5,0 +5,0 @@ "main": "dist/cjs.js", |
@@ -1,6 +0,14 @@ | ||
import { useState } from "react"; | ||
import { useState, useRef, useEffect } from "react"; | ||
const useSuspense = fn => { | ||
const [task, setTask] = useState(null); | ||
const mounted = useRef(true); | ||
useEffect( | ||
() => () => { | ||
mounted.current = false; | ||
}, | ||
[] | ||
); | ||
if (task) { | ||
@@ -14,6 +22,10 @@ throw task; | ||
.then(() => { | ||
setTask(null); | ||
if (mounted.current) { | ||
setTask(null); | ||
} | ||
}) | ||
.catch(() => { | ||
setTask(null); | ||
if (mounted.current) { | ||
setTask(null); | ||
} | ||
}) | ||
@@ -20,0 +32,0 @@ ); |
234910
643