Getting Started
On this page: How to install template & use
Before using thist template, I suggest to learn ReactJS
Requirements
Installation
- Clone repository
git clone https://github.com/KPGTB/fivem-react-template.git
You can also download that template or create a new repository from it.
- Open terminal in
web/react
and install node packages
npm i
- Change name, description, etc. in
- fxmanifest.lua
- web/react/package.json
- web/react/package-lock.json
- web/react/src/index.html
- Edit your code like you want
- After changes in
web/react
you need to build react project
npm run build
- Don't use Webpack Serve. Always build project and check it in-game
- You need to import every stylesheet into
web/styles/main.css
. Don't import styles into React components!
@import url(PATH_TO_CSS_FILE);
- All asset paths must depend on
web/build/index.html
file. Don't import assets into React components!
Examples:
- First Example
- Second Example
- Image path:
web/assets/image.png
- Path to image in every component:
../assets/image.png
- Image path:
web/assets/inventory/icons/apple.png
- Path to image in every component:
../assets/inventory/icons/apple.png
Project structure
- client - Client-side Lua scripts
- client.lua - Example of NUI toggling
- react.lua - Lua utils of react-fivem connection
- server - Server-side Lua scripts
- web - NUI
- assets - NUI assets like images
- logo.png - Example asset
- styles - NUI styles (CSS)
- main.css - Main CSS file
- app.css - Example App component Style
- build - Folder with built node project
- react - ReactJS front-end
- src - Source of node project
- components - Components of ReactJS
- pages - Pages of ReactJS (Components that represent pages)
- utils - Some utils
- FiveM.js - JS utils of react-fivem connection
- App.jsx - File with basic ReactJS component + Small example
- src - Source of node project
- assets - NUI assets like images
Utils
In this template, I added some utilities that help with the react-fivem connection.
Lua Utils (client-side)
- Sending messages to ReactJS
SendReactMessage(action, data)
action - Text with name of action (It will be used in JS)
data - Data of message. It can be text, number, boolean, table, etc.
Example:
SendReactMessage("hello", "world")
That function will send a message with the name hello
and the data world
to ReactJS. See JS Utils
section to check receiving message in JS
- Setting visibility of NUI
SetReactVisible(visible)
visible - true or false
- Toggling visibility of NUI
ToggleReactVisible()
- Checking if NUI is visible
IsReactVisible()
JS Utils
To change default visibility, go to web/react/src/App.jsx
and change state value visible
- Handling messages from NUI
useNui(action, handler)
action - Name of action
handler - Function that will be executed after message from FiveM
Example:
useNui("hello", (data) => console.log(data))
That function will print into the console data received from a message with the name hello
. See Lua Utils (client-side)
section to check sending messages from Lua. In that case, it should print world
- Sending data to Lua
callNui(action, data, handler)
action - Name of NUI callback
data - Data to send
handler - Function that will be invoked after callback
Example:
LUA
RegisterNUICallback("example", function(data,cb)
print(data)
cb("sth")
end)
JS
callNui('example', "test", (data) => console.log(data))
What function will do:
- FiveM will register callback with name
example
- ReactJS will send
test
to FiveM's callback with nameexample
- FiveM will print
example
into console and send a callback with textsth
- ReactJS will print into console response
sth
Examples in code
In that template, I added some examples. Remove it when creating a new resource.
client/client.lua
- lines 1-3web/assets/logo.png
web/styles/app.css
(don't forget to also remove the import fromweb/styles/main.css
)web/react/App.jsx
- lines 13-14