Skip to content

useSend & XRequest ๐ŸŒณ โ€‹

XRequest is deprecated, recommend using hook-fetch (https://jsonlee12138.github.io/hook-fetch/) โ€‹

Background Introduction โ€‹

Based on ant-design-x's XRequest and XStream, we conducted in-depth learning and discussion.

After replicating XStream, for more general scenarios of controlling request data and aborting requests, we refactored ant-design-x's XRequest and split it into Frontend Termination Scenarios and Request Termination Scenarios

These two scenarios correspond to:

  • hooks useSend -- Frontend termination scenarios
  • utility class XRequest -- Request termination scenarios

๐Ÿ’ Both can be used separately, and when combined they can implement useXStream. Below are their usage examples

Code Demonstration โ€‹

You only need to pass a start method to get the corresponding loading state and finish method.

Single control, code doesn't exceed 10 lines

This example clearly shows that this hook doesn't interact with backend requests, it only controls simple loading state.

send method triggers sendHandler callback finish method ends loading state

With control over the state, we can easily customize loading states for some buttons

sendHandler and abortHandler are two functions, corresponding to the callbacks for start and abort.

The abort method triggers the abortHandler callback.

The abort method will also end the loading state.

After understanding the basic usage of useSend, since there's control over frontend loading state, there must also be request state control. Next, let's introduce the simple usage of the utility class XRequest.

This way you can simply use XRequest to control the initiation and aborting of requests and their states. A simple new XRequest() instantiates the object, then .send() starts the request, .abort() stops the request.

You can check the console to see that the streaming request has been terminated.

๐Ÿ“Œ Warning

Here, to make it convenient for everyone to read the documentation and see the requests, we've written a simple node service. In this example, ๐Ÿ’ฉ please don't click frantically. It will make frantic requests to the interface, please be moderate. ๐Ÿ’ฉ We haven't done any security processing ๐Ÿ™‰ because we won't

This can also help everyone better understand the usage of the utility class XRequest, which only handles requests.

Pass the corresponding configuration items in new XRequest()

transformer can process and transform the response data, and many configuration callback methods are also provided for developers to use.

You can also check the console to see the printout of the callback methods.

Below, let's introduce the combined usage of useSend and useSendStream

Use useSend to control frontend state, use useSendStream to control backend state

Configuration Parameters and Return Hooks โ€‹

- useSend โ€‹

  • Parameters
Parameter NameDescriptionType
sendHandlersend method() => void
abortHandlerabort method() => void
  • Return Value
Property NameDescriptionType
sendStart loading state, supports callback() => void
abortAbort loading state, supports callback() => void
loadingLoading stateboolean
finishEnd loading state() => void

- XRequest โ€‹

  • Parameters
Config Parameter NameDescriptionType
baseURLBase request URLstring
typeRequest type, default SSEBaseSSEProps<T = string>.type?: SSEType | undefined
transformertransformer callback, where you can parse and process data(e: string) => string | undefined
onMessageCallback during request(msg: string | undefined) => void
onErrorError callback(es: EventSource, e: Event) => void
onOpenSSE Open stateSSEWithSSEProps.onOpen?: (() => void) | undefined
onAbortCallback when request is aborted(messages: (string | undefined)[]) => void
onFinishCallback when request ends(data: (string | undefined)[]) => void
  • Return Value
Property NameDescriptionType
sendStart request interfaceXRequest<string | undefined>.send(url: string, options?: EventSourceInit | BaseFetchOptions): Promise<XRequest<string | undefined>>
abortAbort requestXRequest<string | undefined>.abort(): void

Summary โ€‹

useSend allows users to more conveniently display and control loading states on the frontend. It's a packaging solution for loading states. It accepts a send callback and an abort callback, provides send, abort loading state, end loading state, and returns loading state.

XRequest is a packaging of requests, providing a more convenient way to make requests. It accepts a request configuration and returns a request response object.