Lesson 4 of 6

UI frameworks, part 2: the React-lua shop, Fusion, Vide and when to use one

Part 2 of 2 on Roblox UI frameworks, walking through Beacon Yard's m09-react-shop in React-lua, adding a pure reducer pattern, and comparing Fusion, Vide, roblox-ts and plain Instances honestly.

Project lesson, about 65 minutes, 9 steps

By the end you can

  • Read Beacon Yard's m09-react-shop implementation (ShopGui, ShopApp, ShopButton and ShopLabels) and extend it with a pure reducer
  • Keep purchases on the server while React-lua UI shows pending, bought and denied states
  • Compare React-lua, Fusion, Vide, roblox-ts and plain Instances, and decide when a framework is worth it

Before this: UI frameworks, part 1: React-lua, elements, props and state

1The shop, as a tree of components

Part 1 installed React-lua and introduced elements, props, state and effects. Now look at the whole shop in m09-react-shop. init.client.luau still calls ShopGui.start(), which builds the BeaconShop ScreenGui, attaches UiScale, and portals ShopApp into it. Inside src/client/ShopApp.luau, ShopApp owns the state (open, coins, textScale) with React.useState and renders a Frame with one ShopButton per item:

text
ShopGui.start() -> ScreenGui "BeaconShop" (UiScale)
└─ ShopApp (useState: open, coins, textScale; useRef: popRef, speedRef)
   ├─ Title       "Quartermaster"
   ├─ Wallet      ShopLabels.wallet(coins)
   ├─ ShopButton  Speed     ShopLabels.item(...)
   ├─ ShopButton  Shield    ShopLabels.item(...)
   ├─ ShopButton  CoinPack  ShopLabels.pack(...)
   └─ Close       TextButton

The prices still come from Config, the Coin Pack still checks Ids.COIN_PACK_PRODUCT_ID, and pure button text formatting lives in src/shared/ShopLabels.luau. Nothing about the server changes.

local function ShopApp(props: ShopProps)
	local player = Players.LocalPlayer
	if player == nil then
		error("ShopApp must run on the client")
	end
 
	local open, setOpen = React.useState(false)
	local coins, setCoins = React.useState(readCoins(player))
	local textScale, setTextScale = React.useState(Accessibility.textScale())
	local popRef = React.useRef(nil :: UIScale?)
	local speedRef = React.useRef(nil :: TextButton?)
Figure 1.1

ShopButton in ShopApp.luau replaces the old hand-built buyButton helper from m06-drone-wrench, and adds a focus ring driven by its own focused state.

Which component owns the open flag in ShopApp.luau?

  1. ShopApp, the top of the tree, because Close, CloseShop and the ShopOpen remote all change it
  2. Each ShopButton separately
  3. The Title
  4. The server
Answer

ShopApp, the top of the tree, because Close, CloseShop and the ShopOpen remote all change it. The Close button, the gamepad CloseShop binding and the ShopOpen handler all change open, and the Frame reads it. Their common parent is ShopApp, so the state lives there and flows down as props.

Open this step in lesson mode · Link to this step · This confused me

2Growing past useState: one reducer for every shop change

m09-react-shop keeps ShopApp small with three useState calls (open, coins, textScale) and lets SoundClient show toast messages for Feedback. Once a screen also needs a pending state (disabling the button while waiting for the server) and an inline status message, several useState setters have to stay in sync. A reducer solves that: it is a pure function that takes the current state and an action and returns the next state. React.useReducer runs it for you, and because it is pure, you can test it without Studio (as you will in the challenges).

Luau
-- The shop's reducer: (state, action) -> new state. Never edits the old one.
local function reduce(state, action)
	if action.type == "open" then
		return { open = true, pending = nil, message = nil }
	elseif action.type == "close" then
		return { open = false, pending = nil, message = nil }
	elseif action.type == "buy" then
		return { open = state.open, pending = action.item, message = nil }
	elseif action.type == "feedback" then
		return { open = state.open, pending = nil, message = action.text }
	end
	return state
end
 
local s0 = { open = false }
local s1 = reduce(s0, { type = "open" })
local s2 = reduce(s1, { type = "buy", item = "Speed" })
local s3 = reduce(s2, { type = "feedback", text = "Not enough coins" })
local function show(...)
	local parts = {}
	for i = 1, select("#", ...) do
		parts[i] = tostring((select(i, ...)))
	end
	print(table.concat(parts, " "))
end
show(s2.pending, s3.pending, s3.message)
show(s0.open, s1.open, s1 == s0)
Figure 2.1. Expected output: Speed nil Not enough coins false true false

After the four lines above, the player presses Close. What is pending?

local s4 = reduce(s3, { type = "close" })
show(s4.pending, s4.open)
  1. nil false
  2. Speed false
  3. nil true
  4. Speed true
Answer

nil false. close returns a fresh table with open false and no pending item. The old states are untouched, which is what lets React compare them.

Open this step in lesson mode · Link to this step · This confused me

3Wiring remotes in effects

This is a sketch, not the repo's code: m09-react-shop has no ShopScreen, Panel or useReducer, and its ShopApp uses three useState calls. The sketch shows how the reducer version of the shop would connect two remotes in effects and send one. reduce is the function from the cell above, and Panel is a component you would write to draw the shop from state. ShopOpen dispatches open. Feedback dispatches feedback with the text the server sent. A click dispatches buy and fires ShopBuy.

luau
-- Sketch: assumes `React`, `e = React.createElement`, `reduce` and `Panel` exist.
local function ShopScreen(props)
	local state, dispatch = React.useReducer(reduce, { open = false })
 
	React.useEffect(function()
		local opened = props.shopOpen.OnClientEvent:Connect(function()
			dispatch({ type = "open" })
		end)
		local replied = props.feedback.OnClientEvent:Connect(function(kind, text)
			if kind == "buy" or kind == "deny" then
				dispatch({ type = "feedback", text = text })
			end
		end)
		return function()
			opened:Disconnect()
			replied:Disconnect()
		end
	end, {})
 
	local function buy(item)
		dispatch({ type = "buy", item = item })
		props.shopBuy:FireServer(item)
	end
 
	return e(Panel, { state = state, buy = buy, close = function()
		dispatch({ type = "close" })
	end })
end

You would mount it once from the client entry script with ReactRoblox.createRoot and a portal into PlayerGui, the way ShopGui.start() mounts ShopApp in the build along of part 1.

The buy button still only writes a note to the front desk. The reducer's pending state is the UI admitting it is waiting for the clerk. The coins on screen do not move until the server's reply arrives.

Think of it as A note to the front-desk clerk

Both sidesRemember: A RemoteEvent is a note to the front-desk clerk.

The server's Feedback remote sends a kind and an optional text. Which kinds should end the pending state in the shop?

  1. buy
  2. deny
  3. coin
  4. swing
  5. start
Answer

buy; deny. ShopService answers a purchase with buy (and the item name) or deny (and the reason). coin, swing and start are other gameplay events that also travel on Feedback.

Open this step in lesson mode · Link to this step · This confused me

4Where the front desk breaks, again

Where does it break?“A note to the front-desk clerk” is a good picture of a remoteevent. Where does it stop being true?

It breaks here. The engine stamps the sender for you, but every other word on the note can be a lie.

The true model. A RemoteEvent carries a one-way message between a client and the server. The server always learns which player sent it; every other argument is untrusted input that the server must check.

A React UI is still a client. A cheater can call dispatch, fake a bought state, or fire ShopBuy with any string. None of that matters, because ShopService checks the item name, the price and the balance on the server. Moving the UI to React changes how the screen is drawn, never who decides.

An exploiter edits the React tree so the Speed button reads 0 coins and fires ShopBuy("Speed"). What happens?

  1. The server charges the real 25 coins, or denies the purchase if the balance is short
  2. Speed is bought for free
  3. React refuses to render the edited label
  4. Every player's shop shows 0 coins
Answer

The server charges the real 25 coins, or denies the purchase if the balance is short. buySpeed calls DataService.trySpend(player, Config.SPEED_PRICE). The label is decoration; the server reads the price from Config.

Open this step in lesson mode · Link to this step · This confused me

5Fusion, Vide, and plain Instances

React is not the only way to describe UI from state. Here is the same coin label in two other libraries.

luau
-- Fusion 0.3: values live in a scope; Computed reads them with use().
local scope = Fusion.scoped(Fusion)
local coins = scope:Value(0)
scope:New "TextLabel" {
	Text = scope:Computed(function(use)
		return "Coins: " .. use(coins)
	end),
}
coins:set(5)
 
-- Vide: a source is a value you call; a function prop re-runs when it changes.
local coins = vide.source(0)
vide.create "TextLabel" {
	Text = function()
		return "Coins: " .. coins()
	end,
}
coins(5)

Fusion and Vide are reactive: they track which values each property reads and update only that property, with no tree comparison. React re-runs components and compares descriptions. Both work well; they suit different teams12.

Other ways pros do this

OptionWhat it isWhen pros pick itTrade-offs
React-luaA Luau port of React 17 (Roblox/react-luau and jsdotlua/react-lua): components, hooks, reconciliation.Large UIs, teams with web React experience, Roblox's own tooling and testing.Heaviest of the four; React concepts to learn; re-renders need care in hot paths.
FusionA reactive state and UI library; version 0.3 introduced scopes for cleanup.Luau-first teams who like fine-grained reactivity and declarative Instances.Pre-1.0, and APIs have changed between versions.
VideA small reactive library inspired by Solid, fully type-checkable.Performance-minded projects that want minimal API and strong types.Smaller community and fewer learning resources.
roblox-ts with @rbxts/reactTypeScript compiled to Luau, with JSX for React-lua.Studios that hire web developers or want npm-style packages.A second language and compiler between you and Roblox.
Plain InstancesInstance.new and property writes, as Beacon Yard's HUD still does.A HUD of a few labels, a prototype, or one screen that rarely changes.Every state change is a handwritten update, and it grows messy fast.

Match each situation to the most sensible choice.

  1. A 5-label HUD that changes twice per round
  2. A 30-screen inventory and trading UI built by a team with web React experience
  3. A solo Luau developer who wants fine-grained reactivity and strict types
  4. A studio whose whole codebase is TypeScript
Answer

A 5-label HUD that changes twice per round: Plain Instances; A 30-screen inventory and trading UI built by a team with web React experience: React-lua; A solo Luau developer who wants fine-grained reactivity and strict types: Vide; A studio whose whole codebase is TypeScript: roblox-ts with @rbxts/react. A framework pays for itself when there is a lot of state and many screens. A tiny HUD does not need one, and the team's existing skills often decide between the frameworks.

Open this step in lesson mode · Link to this step · This confused me

6When a framework is worth it

Beacon Yard keeps the HUD hand-built and moves only the shop to React. That is deliberate. The HUD has a handful of labels driven by one remote, and rewriting it would add a dependency for no gain. The shop has items, prices, a pending state, denial messages and, later, a game pass, which is where hand-built code starts to tangle.

A useful rule: reach for a framework when the same piece of state appears in more than one place on screen, or when you catch yourself writing "update the label" in several handlers.

Why does Beacon Yard leave the HUD hand-built?

  1. It is small and driven by a single remote, so a framework would add cost without saving code
  2. React cannot render TextLabels
  3. The HUD must run on the server
  4. Hand-built UI is faster to load on every device
Answer

It is small and driven by a single remote, so a framework would add cost without saving code. The HUD has a few labels and one source of updates. The cost of a framework (a dependency, new concepts) only pays off when state is spread across many parts of a screen.

Open this step in lesson mode · Link to this step · This confused me

7Build along: the React-lua shop in m09-react-shop

  • wally.toml
  • default.project.json
  • src/client/ShopGui.luau
  • src/client/ShopApp.luau
  • src/shared/ShopLabels.luau
  1. In the game repo, run git diff --stat m08-crawler m09-react-shop -- src wally.toml default.project.json.

    You should see git lists ShopApp.luau and ShopLabels.luau as new files, wally.toml adds React and ReactRoblox, and ShopGui.luau shrinks from about 370 lines to a thin mount wrapper.

  2. Check out m09-react-shop, run wally install, and read src/client/init.client.luau and src/client/ShopGui.luau.

    You should see init.client.luau still calls ShopGui.start(), and ShopGui.luau creates BeaconShop with UiScale and mounts ShopApp through ReactRoblox.createRoot and createPortal.

  3. Open src/client/ShopApp.luau and src/shared/ShopLabels.luau and trace how ShopButton renders each item and dims buttons you cannot afford.

    You should see ShopApp.luau holds open, coins and textScale with React.useState, subscribes to ShopOpen, leaderstats.Coins and Accessibility in React.useEffect with cleanups, and formats button text through ShopLabels.

  4. Run rojo serve, connect Studio, press Play, open the shop, buy an item, and run game.ServerScriptService.RunGameTests:Invoke() from the server command bar.

    You should see Holding the quartermaster prompt opens BeaconShop, collecting coins updates the Wallet and item labels live, and ServerScriptService.RunGameTests:Invoke() still passes.

Open this step in lesson mode · Link to this step · This confused me

8Challenges

Level 2, Builder

Challenge

Extend ShopApp.luau with the reducer pattern from this lesson: show a pending state on the button that was pressed, clear it on Feedback, and block double clicks while it waits.

Start from
The game at milestone m09 (download)

Done means

  • ShopApp tracks a pending item (with useReducer or useState) and listens to the Feedback remote for buy and deny
  • While an item is pending, its ShopButton shows Waiting and ignores clicks (Active = false)
  • A buy or deny Feedback ends the pending state, and closing the shop clears pending

Hints

  1. Pass a Feedback RemoteEvent into ShopApp from ShopGui.luau, just like shopOpen and shopBuy.
  2. Pass pending down to each ShopButton as a prop and set Active = false while it matches.
  3. Disconnect the Feedback connection in the effect's cleanup.

Level 3, Professional

Challenge

Write the shop's reducer and its label function in the learner file challenges/work/ui_react.luau (start from the m14-ship tag in the game repo to get challenges/work/ui_react.luau and challenges/ui-react.luau). The test needs no React-lua runtime and runs on pure Luau. The written solution is challenges/solutions/ui-react.md.

Start from
The game at milestone m14 (download)
Grade it
In Studio, press Play, then run game.ServerScriptService.RunGameTests:Invoke("ui-react", "ui-react-reducer") in the server command bar. Copy the pass code from Output.

Done means

  • challenges/work/ui_react.luau exports reduce(state, action) and label(name, price, pending, packReady)
  • reduce(state, action) returns a state shaped { open, pending, message } and handles open, buy, feedback, close; unknown actions return the same table, and the input state is never mutated
  • buy sets pending to the item and clears any old message; feedback clears pending and sets message; close clears open, pending and message
  • label returns "Name\nWaiting" when pending == name, "Coin Pack\n50 coins" or "Coin Pack\nset COIN_PACK_PRODUCT_ID" for CoinPack, and "Name\n<price> coins" otherwise
  • challenges/ui-react.luau prints TEST PASS for every check

Hints

  1. Build a new table in every branch instead of assigning to state (the test freezes input tables with table.freeze).
  2. Unknown actions return the same state table.
  3. Check challenges/ui-react.luau for the exact strings: "Speed\n25 coins", "Shield\nWaiting", "Coin Pack\n50 coins", "Coin Pack\nset COIN_PACK_PRODUCT_ID".

Why can the level 3 test check the shop without React or a player?

  1. The reducer and label are pure functions: same inputs, same outputs, no Instances
  2. The test starts a hidden copy of Studio
  3. React has a built-in test mode that runs on the server
  4. The test only checks that the file exists
Answer

The reducer and label are pure functions: same inputs, same outputs, no Instances. Pure functions only need their arguments. Keeping the logic out of the component is what makes it testable outside Studio, and the same idea drives Jest-Lua unit tests later in the course.

Open this step in lesson mode · Link to this step · This confused me

9Recap

Retiring the analogy

You no longer need “a note to the front-desk clerk”. A RemoteEvent carries a one-way message between a client and the server. The server always learns which player sent it; every other argument is untrusted input that the server must check.

Recap

  1. In m09-react-shop, ShopGui.start() mounts ShopApp via ReactRoblox.createRoot and createPortal; ShopApp owns open, coins and textScale with useState and wires remotes and leaderstats in useEffect.
  2. When a screen grows to need pending states and inline messages, a pure reducer with useReducer keeps every transition in one testable function.
  3. React-lua, Fusion, Vide, roblox-ts and plain Instances all work; pick by UI size and team skills, and keep a small HUD hand-built.

A RemoteEvent is a note to the front-desk clerk, until the engine stamps the sender for you, but every other word on the note can be a lie.

Next Audio: recording, editing, licensing, the audio graph

Open this step in lesson mode · Link to this step · This confused me