Giter Club home page Giter Club logo

fivem-react-boilerplate-lua's Introduction

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!

fivem-react-boilerplate-lua's People

Contributors

brentn5 avatar dependabot[bot] avatar depfu[bot] avatar dunak-debug avatar itschip avatar kevintjuhz avatar liamdormon avatar lukewastakenn avatar r3ps4j avatar tasooneasia avatar xdreamland avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

fivem-react-boilerplate-lua's Issues

Question: How to send data with no NUI callback

I made a vehicle hud that displays speed, gear and rpm when inside a vehicle. It doesn't have any buttons to click, so it doesn't use the native RegisterNUICallback at all. I was wondering how I can send data (such as speed, gear, etc.) back to React from the client-sided Lua code?

Bug: Css hover elements with delay

All elements with hover selector has a delay to disappear from screen

To Reproduce

Steps to reproduce the behavior:

  1. Create any element with hover selector on css
  2. Press esc or backspace

Pressing the 'Backspace' key while the NUI menu is open breaks everything

The issue is when you have the NUI menu open, or any NUI element open, and you press the 'backspace' key it will make the UI disappear and your curser will remain. You need to restart the script to fix it.

There is JSON error in the client console stating there is an issue with the JSON input. (line 1 of the HTML)

--EDIT--

Here is the error code if you need it:

Uncaught (in promise) SyntaxError: Unexpected end of JSON input (@scriptname/web/build/index.html:1)

Could not auto-determine entry point from rollupOptions

Describe the issue

Hi, i'm trying to use this boilerplate but if i try to run this command yarn start start:game i get the next message (image -1.0) so i cant be able to make the build of my project. This is a blank project

Expected behavior

Get the new build resources after change has made

To Reproduce

Steps to reproduce the behavior:

  1. Run yarn start start:game
  2. See warning

Media

image
Image 1.0 (Warning message)

image
Image 1.1 (package.json)

image
Image 1.2 (vite.config.ts)

Question : Is an update needed to match the new React stuff ?

Hey !

I'm wanting to start my first React project in FiveM but I've some questions that came up while reading React's official documentation. It seems that some things have changed a lot lately in React and I would like to know if there would be needed to update this boilerplate to match the latest standards ?

Just wanna know if I can start learning without the hassle to update this later

Thanks in advance ! :)

Depfu Error: No dependency files found

Hello,

We've tried to activate or update your repository on Depfu and couldn't find any supported dependency files. If we were to guess, we would say that this is not actually a project Depfu supports and has probably been activated by error.

Monorepos

Please note that Depfu currently only searches for your dependency files in the root folder. We do support monorepos and non-root files, but don't auto-detect them. If that's the case with this repo, please send us a quick email with the folder you want Depfu to work on and we'll set it up right away!

How to deactivate the project

  • Go to the Settings page of either your own account or the organization you've used
  • Go to "Installed Integrations"
  • Click the "Configure" button on the Depfu integration
  • Remove this repo (project-error/fivem-react-boilerplate-lua) from the list of accessible repos.

Please note that using the "All Repositories" setting doesn't make a lot of sense with Depfu.

If you think that this is a mistake

Please let us know by sending an email to [email protected].


This is an automated issue by Depfu. You're getting it because someone configured Depfu to automatically update dependencies on this project.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.