diff --git a/README.md b/README.md index 3730ad8..33ccac8 100644 --- a/README.md +++ b/README.md @@ -15,3 +15,12 @@ Since the state hasn't been removed since it had been captured by the effect, th take a note in the effect that you're currently mounted. If you're unmounted the effects returned callback is called. Set the unmount state there. Check in the promise resolution if we're already unmounted and dismiss the effect without changing state. + +# react async solution + +comes with lots of options how to express yourself: https://docs.react-async.com/getting-started/usage +`PageOne`: demo with a `useAsync` hook +`PageTwo`: demo with context based Async components +(more in the docs) + +React Async claims to be fully compatible to React Suspense / Concurrent React once it has landed. diff --git a/package.json b/package.json index 4e72549..fa17300 100644 --- a/package.json +++ b/package.json @@ -7,6 +7,7 @@ "@testing-library/react": "^9.3.2", "@testing-library/user-event": "^7.1.2", "react": "^16.13.1", + "react-async": "^10.0.1", "react-dom": "^16.13.1", "react-router": "^5.2.0", "react-router-dom": "^5.2.0", diff --git a/src/PageOne.js b/src/PageOne.js index 92bbef4..b989861 100644 --- a/src/PageOne.js +++ b/src/PageOne.js @@ -1,19 +1,15 @@ -import React, {useEffect, useState} from 'react'; +import React from 'react'; +import { useAsync } from "react-async" -function aVeryHeavyAsyncApiCall(thatRunsForMilliSeconds) { +function aVeryHeavyAsyncApiCall({thatRunsForMilliSeconds}) { return new Promise((resolve, reject) => { - setTimeout(() => resolve("hey, Im done!"), thatRunsForMilliSeconds); + setTimeout(() => resolve(`hey, I ran ${thatRunsForMilliSeconds}ms!`), thatRunsForMilliSeconds); }) } function PageOne() { - const [apiResult, setApiResult] = useState(); - - useEffect(() => { - aVeryHeavyAsyncApiCall(3000).then(result => { - setApiResult(result) - }) - }, []) + + const { data: apiResult } = useAsync({ promiseFn: aVeryHeavyAsyncApiCall, thatRunsForMilliSeconds: 3000 }) return (