Open TheEpicFace007 opened 2 years ago
How can I do functional components with roact?
Is there a such thing such a useState and useEffect?
useState
useEffect
Here is how I think I should do it:
---@type Roact local Roact = getrenv().require(game:GetService("CorePackages")["Packages"]["_Index"]["roblox_roact"]["roact"]) ---@type Roact local React = Roact local function Button(props) local count = Roact.useState(value) return React.createElement("TextButton", { Text = ("Click count: %d"):format(count.value), [Roact.Event.MouseButton1Click] = function () count.setState(count + 1) end }) end local function App() return Roact.createElement("Frame", { Size = UDim2.fromScale(0.3, 0.4), AnchorPoint = Vector2.new(0, 1), Position = UDim2.fromScale(0.1, 0.9), }, { React.createElement("TextLabel", { Text = "Bible Bot", AnchorPoint = Vector2.new(0, 0), Position = UDim2.fromScale(0.5, 0.05), TextSize = 15 }), Button({}) }) end Roact.unmount(getgenv().app) getgenv().app = Roact.mount(Roact.createElement("ScreenGui", {}, App()), game.CoreGui)
Someone has started working on a roact hooks library here: https://github.com/Kampfkarren/roact-hooks idk if anyone is planning on adding it to the roact library
How can I do functional components with roact?
Is there a such thing such a
useState
anduseEffect
?Here is how I think I should do it: