How To: React + Roblox

I’m having a problem with react, I’m trying to create useState, and it gives me an error

I’ve searched everything and found nothing, if someone could tell me the problem, code below:

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local React = require(ReplicatedStorage.Packages.react)

export type Props = {
  Name:string,
  Position:UDim2?,
  ImageId:string,
  HoverImageId:string,
  LayoutOrder:number,
}


function CreateButton(Props:Props)
  local hover, SetHover  = React:useState({state = false})

  local ImageId = Props.ImageId
  local HoverImageId = Props.HoverImageId

  local DefaultColor = Color3.fromHex("363636")
  local HoverColor = Color3.fromRGB(255,255,255)
  return React.createElement("ImageButton",{
    LayoutOrder = Props.LayoutOrder,
    Name = `{Props.Name}Button`,
    Size = UDim2.fromOffset(498,62),
    Position = Props.Position or UDim2.fromScale(0,0),
    AnchorPoint = Vector2.new(0,0),
    BackgroundTransparency = 1,
    Image = ImageId,
    ScaleType = Enum.ScaleType.Fit,
    [React.Event.MouseEnter] = function()
      SetHover({state = true})
    end,
    [React.Event.MouseLeave] = function()
      SetHover({state = false})
    end,
  },{ React.createElement("UIAspectRatioConstraint",{AspectRatio = 8.04}),
      React.createElement("TextLabel",{
        Name = `{Props.Name}Text`,
        AnchorPoint = Vector2.new(0.5,0.5),
        Position = UDim2.fromScale(0.5,0.5),
        Size = UDim2.fromScale(0.8,0.8),
        BackgroundTransparency = 1,
        Text = Props.Name,
        TextColor3 = DefaultColor,
        TextScaled = true,
        Font = Enum.Font.SourceSansSemibold,
      })
    })
end


return CreateButton

If anyone can help, I’d be very grateful

A little late so hopefully you fixed by now but the issue was that you are using colon syntax for useState when it should be using dot syntax.

That is, it should be

React.useState({}) -- NOT React:useState({})

For faster react-lua help, join the Roblox OSS discord. There is a react channel which is pretty active and great place to ask for questions.

it’s a terrible take… the point of a ui library is to optimize the way you create UI. coding Ui is the standard outside of Roblox at least, and think of this. your method is just using plain html and css while a ui library is using js to better control your UI and the way it behaves, which leads to cleaner code. there’s a significantly higher learning curve but it’s more beneficial in the end. although i do agree react kinda sucks for roblox and its not as efficient as libraries like vide or lumin ui

As someone who’s been developing on the platform for several years now, I was also weary about using a UI library, as I shared similar opinions to those who are against using it (“it’s useless!”)

Switching to React was the best decision I’ve ever made and I couldn’t imagine making a game without it now.

React makes managing massive projects 1000x easier, and it does all the hard work for you.

I use Roblox-TS which makes using React even more pleasant. I would highly suggest anyone who is weary about using React to atleast give it a go - and if your current workflow is working, don’t use it! Use whatever you’re comfortable with.

2 Likes

Im someone who wants to give a UI library a shot, however I’ve got no idea how its better than actually creating the UI visually since you get to see what it looks like and stuff. Where as when you code it you don’t get to visually see it unless you imagine it. Simply put, I just want to know how you actually use it, like the process of using it.

This was one of my main worries too, being unable to actually visually see what I’m making.

You don’t need to use the library to design your UIs - I design my UI in studio as you usually would, and then manually translate it into code. Doing this allows me to see what I’m making and not have to worry about the programmed version looking bad, if anything, the programmed version always comes out tidier due to reusing components, like a button

1 Like

Hi!

If you wish to visualize the user interfaces you code, I recommend this Roblox UI Lab Plugin.

UI Lab is a powerful, user-interface friendly storybook development plugin which allows you to preview the user interface on Roblox Studio while you code on Visual Studio Code.

To get started, you’ll need to write your own story and use the plugin to display user interface using your story, learn more on it’s own official documentation about usage. Happy coding!

5 Likes

I honestly wish there was a more straightforward visualizer, because I am honestly just not able to comprehend storybook-based visualizers. If there was one where you select a script, and it would visualize that script’s react code, that would be a godsend.

Is this just me being incompetent, and being unable to wrap my head around that stuff? Yes. Would having a more straightforward visualizer still help? Also yes.

1 Like

Trust me, a storybook-based plugin is not as hard-to-use as you think!

The reason why the process is not straightforward is because the plugin does not know which part of the UI to render, even if you select the entire script.

I suggest that you create a new script, ending with the extension .story, as these scripts are only detected by the plugin.

The plugin will then try create a element from that script to render, so your script should have the component which returns the elements (You can even duplicate the original UI if you want!).

The exact steps? Might vary, so I suggest reading the documentation I linked above!

1 Like

So, I wanted to suggest something that could make react-lua easier to learn, and it’s specifically related to documentation. (Since I don’t have feature request permissions, I’ll post this here since I don’t know where else to.)

The react-lua docs should have code examples/editors that dynamically change when you change the code. They give developers a way to interact with react code without having to run their games or use storybook plugins, which can help a LOT, especially for beginners.

Adding this to the react-lua docs would make react-lua more accessible, especially since react-lua code is very dense compared to regular react code.