useNuiQuery
Overview
The useNuiQuery
hook sends an NUI callback to the FiveM client-side to retrieve data. It simplifies the process by allowing you to include a request body with various data types without the need for manual string conversion or parsing.
Use useNuiMutation
for sending data (e.g., handle click), as it's designed for mutations, while useNuiQuery
is for fetching data.
Usage
Importing the Hook
Parameters
endpoint
string
Endpoint where you send NUI Callback.
options
Additional options
Options
Object contains parameters below (all parameters are optional):
body
string | number | boolean | Array<unknown> | Record<string, unknown>
The body
parameter contains the data sent with the NUI request, supporting various types without requiring manual string conversion.
onSuccess
(value: Response | void) ⇒ void | PromiseLike<void>
The onSuccess
callback is triggered on a successful NUI response, receiving the data for further processing.
onError
(reason: unknown) ⇒ void | PromiseLike<void> | null | undefinded
The onError
callback is triggered if the NUI request fails, allowing error handling or recovery actions.
onFinally
() ⇒ void | null | undefinded
The onFinally
callback runs after the NUI request completes, regardless of success or failure, for cleanup or final actions.
Return Value
This hook does not return a value but automatically send a NUI Callback where you can handle diffrent states of response in options.
Example
ReactJS
LUA
How It Works
The React component (
MyComponent
) callsuseNuiQuery
with the event name (/garage-vehicles
) and an options object containing abody
parameter ({ page }
) and success/error handlers.Inside
useNuiQuery
, theuseEffect
hook triggers thefetchNui
function, sending an NUI message to the FiveM client with the specifiedendpoint
(/garage-vehicles
) and request body ({ page }
).On the FiveM side, the
RegisterNUICallback
function listens for the/garage-vehicles
event. When triggered, it retrieves the requestedpage
from the data and responds with the corresponding vehicle list fromtestVehicles
.The response is returned to the React frontend, where the
.then(options?.onSuccess)
handler processes the data. TheonSuccess
function attempts to parse the response JSON and updates thevehicles
state usingsetVehicles(data)
.The component re-renders, displaying the list of vehicles in
.vehicles-container
. Each vehicle is shown inside.vehicle-card
, with itsname
andplate
.If an error occurs, the
onError
callback triggers an alert with the message "Something went wrong."
Notes
The hook sends an NUI callback to the FiveM client when the
endpoint
oroptions
change.It uses
useTransition
to keep the UI responsive while processing the request.The provided callbacks (
onSuccess
,onError
,onFinally
) allow flexible handling of the response lifecycle.The hook automatically manages the request lifecycle without requiring manual cleanup.
The
isPending
state helps track the request status for UI updates.
Conclusion
The useNuiQuery
hook provides a streamlined way to send NUI requests in a React-based FiveM environment. It abstracts the request logic, supports structured callbacks, and improves UI responsiveness with useTransition
.
Last updated