Last active
March 3, 2023 19:09
-
-
Save jbachhardie/7eb2448fcf873c03c2100b333fe75940 to your computer and use it in GitHub Desktop.
Safe Async in React
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const action = { | |
type: "FETCH_RESOURCE", | |
}; | |
const asyncAction = () => createAsyncAction(action, fetchResource); | |
const isPending = makeIsPendingSelector(action.type, '', true); | |
const isFailed = makeErrorSelector(action.type) | |
function AsyncComponent() { | |
const dispatch = useDispatch() | |
const data = useSelector(selectData) | |
const loading = useSelector(isPending) | |
const error = useSelector(isFailed) | |
useEffect(() => dispatch(asyncAction())) | |
if (error) throw error; | |
if (loading) return <Loading />; | |
if (data) return <Display data={data} />; | |
throw new Error("No data or error received"); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function AsyncComponent() { | |
const { loading, data, error } = useQuery(RESOURCE_QUERY) | |
if (error) throw error | |
if (loading) return <Loading /> | |
if (data) return <Display data={data} /> | |
throw new Error("No data or error received") | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function AsyncComponent() { | |
const [loading, setLoading] = useState(true); | |
const [error, setError] = useState(); | |
const [data, setData] = useState(); | |
useEffect(() => | |
(async () => { | |
if (loading) { | |
try { | |
const response = await fetchResource(); | |
setLoading(false); | |
setData(response.data); | |
} catch (err) { | |
setLoading(false); | |
setError(error); | |
} | |
} | |
})() | |
); | |
if (error) throw error; | |
if (loading) return <Loading />; | |
if (data) return <Display data={data} />; | |
throw new Error("No data or error received"); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function AsyncComponent() { | |
const [loading, setLoading] = useState(true) | |
const [error, setError] = useState() | |
const [data, setData] = useState() | |
useEffect(async () => { | |
if (loading) { | |
try { | |
const response = await asyncAction() | |
setLoading(false) | |
setData(response.data) | |
} catch (err) { | |
setLoading(false) | |
setError(error) | |
} | |
} | |
}) | |
if (error) throw error | |
if (loading) return <Loading /> | |
if (data) return <Display data={data} /> | |
throw new Error("No data or error received") | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function AsyncComponent() { | |
const [loading, setLoading] = useState(true); | |
const [error, setError] = useState(); | |
const [data, setData] = useState(); | |
useEffect(() => { | |
if (loading) { | |
const response = fetchResource | |
.then(() => { | |
setLoading(false); | |
setData(response.data); | |
}) | |
.catch((err) => { | |
setLoading(false); | |
setError(error); | |
}); | |
} | |
}); | |
if (error) throw error; | |
if (loading) return <Loading />; | |
if (data) return <Display data={data} />; | |
throw new Error("No data or error received"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment