• Stars
    star
    208
  • Rank 189,015 (Top 4 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 3 years ago
  • Updated 6 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Basic Lua & React (TypeScript) boilerplate for FiveM
Material-UI logo

FiveM React and Lua Boilerplate

A simple and extendable React (TypeScript) boilerplate designed around the Lua ScRT

license Discord David Dependabot Status

This repository is a basic boilerplate for getting started with React in NUI. It contains several helpful utilities and is bootstrapped using create-react-app. It is for both browser and in-game based development workflows.

For in-game workflows, Utilizing craco to override CRA, we can have hot builds that just require a resource restart instead of a full production build

This version of the boilerplate is meant for the CfxLua runtime.

Requirements

A basic understanding of the modern web development workflow. If you don't know this yet, React might not be for you just yet.

Getting Started

First clone the repository or use the template option and place it within your resources folder

Installation

The boilerplate was made using yarn but is still compatible with npm.

Install dependencies by navigating to the web folder within a terminal of your choice and type npm i or yarn.

Features

This boilerplate comes with some utilities and examples to work off of.

Lua Utils

SendReactMessage

This is a small wrapper for dispatching NUI messages. This is designed to be used with the useNuiEvent React hook.

Signature

---@param action string The action you wish to target
---@param data any The data you wish to send along with this action
SendReactMessage(action, data)

Usage

SendReactMessage('setVisible', true)

debugPrint

A debug printing utility that is dependent on a convar, if the convar is set this will print out to the console.

The convar is dependent on the name given to the resource. It follows this format YOUR_RESOURCE_NAME-debugMode

To turn on debugMode add setr YOUR_RESOURCE_NAME-debugMode 1 to your server.cfg or use the setr console command instead.

Signature (Replicates print)

---@param ... any[] The arguments you wish to send
debugPrint(...)

Usage

debugPrint('wow cool string to print', true, someOtherVar)

React Utils

Signatures are not included for these utilities as the type definitions are sufficient enough.

useNuiEvent

This is a custom React hook that is designed to intercept and handle messages dispatched by the game scripts. This is the primary way of creating passive listeners.

Note: For now handlers can only be registered a single time. I haven't come across a personal usecase for a cascading event system

Usage

const MyComp: React.FC = () => {
  const [state, setState] = useState('')
  
  useNuiEvent<string>('myAction', (data) => {
    // the first argument to the handler function
    // is the data argument sent using SendReactMessage
    
    // do whatever logic u want here
    setState(data)
  })
  
  return(
    <div>
      <h1>Some component</h1>
      <p>{state}</p>
    </div>
  )
}

fetchNui

This is a simple NUI focused wrapper around the standard fetch API. This is the main way to accomplish active NUI data fetching or to trigger NUI callbacks in the game scripts.

When using this, you must always at least callback using {} in the gamescripts.

This can be heavily customized to your use case

Usage

// First argument is the callback event name. 
fetchNui<ReturnData>('getClientData').then(retData => {
  console.log('Got return data from client scripts:')
  console.dir(retData)
  setClientData(retData)
}).catch(e => {
  console.error('Setting mock data due to error', e)
  setClientData({ x: 500, y: 300, z: 200})
})

debugData

This is a function allowing for mocking dispatched game script actions in a browser environment. It will trigger useNuiEvent handlers as if they were dispatched by the game scripts. It will only fire if the current environment is a regular browser and not CEF

Usage

// This will target the useNuiEvent hooks registered with `setVisible`
// and pass them the data of `true`
debugData([
  {
    action: 'setVisible',
    data: true,
  }
])

Misc Utils

These are small but useful included utilities.

  • isEnvBrowser() - Will return a boolean indicating if the current environment is a regular browser. (Useful for logic in development)

Development Workflow

This boilerplate was designed with development workflow in mind. It includes some helpful scripts to accomplish that.

Hot Builds In-Game

When developing in-game, you can use the hot build system by running the start:game script. This is essentially the start script but it writes to disk. Meaning all that is required is a resource restart to update the game script

Usage

# yarn
yarn start:game
# npm
npm run start:game

Production Builds

When you are done with development phase for your resource. You must create a production build that is optimized and minimized.

You can do this by running the following:

npm run build
yarn build 

Additional Notes

Need further support? Join our Discord!

More Repositories

1

npwd

NPWD is a FiveM phone resource written entirely in TypeScript and React.
TypeScript
347
star
2

pefcl

pefcl (PE-Financial) is a financial banking resource for FiveM written in TypeScript and React.
TypeScript
61
star
3

svelte-lua-boilerplate

Svelte & Lua boilerplate for FiveM
TypeScript
48
star
4

fivem-typescript-boilerplate

A simple boilerplate for getting started with TypeScript in FiveM
JavaScript
39
star
5

pe-basicloading

A simple yet robust FiveM loading screen supporting markdown.
JavaScript
26
star
6

pe-fake-plate

Fake plate resource for FiveM that allows players to apply plates to vehicles.
Lua
20
star
7

pe-cli

A FiveM CLI tool to create Javascript/Typescript resources fast and simple.
TypeScript
19
star
8

fivem-lua-style

A mostly reasonable style & convention guide for FiveM's Lua ScRT
15
star
9

qb-pefcl

Lua
13
star
10

pe-utils

A collection of V8 specific utilities used by the Project Error team.
TypeScript
13
star
11

npwd-app-template

Basic app template for NPWD.
TypeScript
12
star
12

cfa-gui

Desktop Application for CFA (Create FiveM App)
TypeScript
10
star
13

pe-typescript

A FiveM TypeScript Boilerplate for creating resources.
JavaScript
7
star
14

projecterror.dev

The landing site and primary documentation site for Project Error ventures.
JavaScript
7
star
15

pefcl-esx

PFCL framework integration for ESX
Lua
7
star
16

pe-ui

WIP General UI Wrapper for FiveM
TypeScript
7
star
17

pe-db

WIP SQL Bridge for FiveM
TypeScript
3
star
18

pe-environment

This is not a framework.
TypeScript
3
star
19

pe-financial

Standalone Banking resource for FiveM written in React and TypeScript
TypeScript
2
star
20

fivem-typescript-prisma

FiveM TypeScript boilerplate with Prisma
JavaScript
2
star
21

pe-utils-examples

1
star
22

pe-bot

TypeScript
1
star
23

pe-core

Lua
1
star
24

fui

A blazingly fast JavaScript library for FiveM NUI resources
JavaScript
1
star
25

fivem-vue-boilerplate

WIP DO NOT USE
Vue
1
star