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
import { useNuiQuery } from "@yankes/fivem-react/hooks";Parameters
endpoint
string
Endpoint where you send NUI Callback.
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
import { useState } from "react";
import { useNuiQuery } from "@yankes/fivem-react/hooks";
export const MyComponent = () => {
const [vehicles, setVehicles] = useState([]);
const [page, setPage] = useState("los-santos");
useNuiQuery("/garage-vehicles", {
body: { page },
onSuccess: await (response) => {
const data = await response.json();
setVehicles(data);
},
onError: () => alert("Something went wrong")
});
return (
<div className="vehicles-container">
{vehicles.map((vehicle) => (
<div class="vehicle-card">
<h3>{vehicle.name}</h3>
<p>{vehicle.plate}</h3>
</div>
))}
</div>
);
};LUA
local testVehicles = {
["los-santos"] = {
{ name = "Blista", plate = "QWE123" },
{ name = "Sentinel Classic", plate = "XYZ123" }
},
["sandy-shores"] = {
{ name = "Kamacho", plate = "123456" }
},
["paleto-bay"] = {}
}
RegisterNUICallback("garage-vehicles", function(data, cb)
local page = data.page
cb(testVehicles[page] or {})
end)
How It Works
The React component (
MyComponent) callsuseNuiQuerywith the event name (/garage-vehicles) and an options object containing abodyparameter ({ page }) and success/error handlers.Inside
useNuiQuery, theuseEffecthook triggers thefetchNuifunction, sending an NUI message to the FiveM client with the specifiedendpoint(/garage-vehicles) and request body ({ page }).On the FiveM side, the
RegisterNUICallbackfunction listens for the/garage-vehiclesevent. When triggered, it retrieves the requestedpagefrom 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. TheonSuccessfunction attempts to parse the response JSON and updates thevehiclesstate usingsetVehicles(data).The component re-renders, displaying the list of vehicles in
.vehicles-container. Each vehicle is shown inside.vehicle-card, with itsnameandplate.If an error occurs, the
onErrorcallback triggers an alert with the message "Something went wrong."
Notes
The hook sends an NUI callback to the FiveM client when the
endpointoroptionschange.It uses
useTransitionto 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
isPendingstate 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