Description:
Are you tired of creating the states in your component such as isLoading, isError, data, retry? Then, use-promise-result library is for you, which is light-weight and simple data fetcher for React component. You will just need to specify how to get data and this component will do everything.
How to use it?
You will need to install the data fetcher for React component with NPM, like this:
npm i use-promise-result
2. Then, import the usePromiseResult in your component.
import { usePromiseResult } from “use-promise-result”;
3. And, the code that will do everything.
const dataProvider = async () => {
return (await axios.get(“”)).data;
};
function App() {
const { data, error, loading, success, reload } =
usePromiseResult(dataProvider);
// …
}
That’s it.
The post A Light-weight And Simple Data Fetcher For React – use-promise-result appeared first on Lipku.com.
