@reactseed/use-request
v1.2.1
Published
Use request with a React hooks
Downloads
22
Readme
@reactseed/use-request
This project is extended based on @umijs/use-request. Support all APIs of @umijs/use-request
.
See the document about @umijs/use-request
API for more information.
Installation
npm install @reactseed/use-request
# or
yarn add @reactseed/use-request
Features
- Built-in axios as the default request method.
- Support Restful style API.
- Support global error message handling and whether to handle error messages.
- Support typescript.
How to use
A simple example:
import React from "react";
import ReactDOM from "react-dom";
import useRequest from "@reactseed/use-request";
const queryData = "https://randomuser.me/api";
const App = () => {
const { data, run, loading } = useRequest(queryData, {
manual: true
});
return (
<>
<button onClick={run} disabled={loading}>
fetch
</button>
<div>{JSON.stringify(data)}</div>
</>
);
};
ReactDOM.render(<App />, document.getElementById("root"));
Use restful API
Request /api/user/1
.
const { data } = useRequest((data) => ({
url: '/api/user/:id',
data: {
id:1,
},
}));
Custom error handling
import { config } from "@reactseed/use-request";
config({
onErrorBefore: error => {
alert(error.messgae);
}
});
Disabled error message
import useRequest from "@reactseed/use-request";
const { data } = useRequest('https://randomuser.me/api1', {
disabledErrorMessage: false
});
Examples of error handling:
License
@reactseed/use-request
is open source software licensed as BDS.