Start
This library is available for React Project
Quick Start
Before using useRequest, create a component used useRequest.
function Component() {
return (
<div>
<div>Data</div>
<button>Upload</button>
</div>
);
};
then use useRequest if you use RESTful API
function Component() {
const { data } = useRequest();
return (
<div>
<div>{data}</div>
<button>Upload</button>
</div>
);
};
write URL where send request
const Component = () => {
const { data } = useRequest('https://jsonplaceholder.typicode.com/posts');
return (
<div>
<div>{data}</div>
<button>Upload</button>
</div>
);
};
And make button send specific request
That's all. useRequest is so easy to use. Not only this.
In this example, even though the button is not pressed, there is already data. The reason is that we hit the upload button above this example.
use-request will sync the values if they are the same URL. Therefore, in this example, the data exists even if the button is not pressed. If you did not press the button in the example above, press any button in the two examples and check the result. The data in the two examples works with each other.
info
Of course you can also send the request individually even if the URL is the same. In this case, you can use cache option and RequestConfig.
This example just added isValidating to the example above. However, the number of renderings increased. use-request trace where properties are used and only re-renders the component when the used properties are updated.
If you don't use isValidating in this example, the component will be rendered twice.