useNuiMutation
Overview
The useNuiMutation
hook provides an easy and effective way to send mutations (i.e., actions that modify data or trigger side effects) to the FiveM client in a React-based environment. It simplifies the process of interacting with the NUI (Native UI) system by sending requests and handling responses with custom callbacks.
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
useNuiMutation
does not require any parameters for initialization. However, when invoking the mutate
function, you have to pass the following 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
The hook returns an object with the following properties:
mutate
A function that accepts an endpoint
string and an optional options
object. The mutate
function sends the NUI mutation request.
isPending
boolean
A boolean state indicating whether the mutation is currently in progress. It can be used to show loading indicators or disable UI elements while the mutation is pending.
Example
ReactJS
LUA
How It Works
The
useNuiMutation
hook initializes amutate
function and aisPending
state.When the
mutate
function is called, it receives anendpoint
and an optionaloptions
object containing data and callbacks.The
mutate
function usesstartTransition
to manage the mutation process, ensuring the UI remains responsive.The
fetchNui
function sends the NUI request to the FiveM client with the specifiedendpoint
andbody
data.If the request is successful, the
onSuccess
callback is triggered, allowing the response to be processed.If an error occurs during the mutation, the
onError
callback is executed to handle the error.After the mutation process completes, regardless of success or failure, the
onFinally
callback is called for cleanup operations.The
isPending
state is updated based on whether the mutation is still in progress, allowing the UI to reflect the current request status (e.g., showing loading indicators or disabling UI elements).
Notes
The hook provides flexible callbacks (
onSuccess
,onError
, andonFinally
) for handling the response lifecycle in a structured manner.The mutation request lifecycle is managed automatically, eliminating the need for manual cleanup.
The
isPending
state helps track the request status, enabling dynamic UI behavior, such as displaying loading spinners or disabling buttons while the request is in progress.The hook abstracts away the complexity of dealing with NUI requests, allowing you to focus on handling the response and UI interactions.
Conclusion
The useNuiMutation
hook provides a straightforward and flexible way to handle mutations within a React-based FiveM environment. It abstracts away the complexity of managing NUI requests, keeping your code clean, responsive, and easy to maintain.
Last updated