Skip to content
This repository has been archived by the owner on Dec 13, 2023. It is now read-only.

Functional components #316

Open
TheEpicFace007 opened this issue Aug 10, 2021 · 1 comment
Open

Functional components #316

TheEpicFace007 opened this issue Aug 10, 2021 · 1 comment

Comments

@TheEpicFace007
Copy link

How can I do functional components with roact?

Is there a such thing such a useState and 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)
@SheepWizard
Copy link

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants