Hooks
Functions
useArray
Hooks.
useArray
(
initialValue:Â
{
T
}
 |Â
nil
) →Â
HookCreator
<
UseArray
<
T
>
>
Types
interface
UseArray<T> {
array:Â
{
T
}
set:Â
(
valueOrCallback:Â
T
 |Â
(
array:Â
T
)
 →Â
T
)
 →Â
void
push:Â
(
...elements:Â
T
)
 →Â
void
filter:Â
(
callback:Â
(
element:Â
T
,
index:Â
number
)
 →Â
boolean
)
 →Â
void
update:Â
(
index:Â
number
,
element:Â
T
)
 →Â
void
remove:Â
(
index:Â
number
)
 →Â
void
clear:Â
(
)
 →Â
void
}
Lets you manipulate an array data structure without ever needing extra utilities.
local function HookedComponent(props, hooks)
local text = hooks.useValue()
local list = useArray({})(hooks)
return Roact.createFragment({
Button = Roact.createElement("TextButton", {
-- ...
Text = "Add",
[Roact.Event.MouseButton1Click] = function(rbx)
if text.value then
list.push(text.value)
end
text.value = nil
end
}),
TextBox = Roact.createElement("TextBox", {
-- ...
Text = "",
[Roact.Change.Text] = function(rbx)
text.value = rbx.Text
end
}),
Display = Roact.createElement("Frame", {
-- ...
}, {
Layout = Roact.createElement("UIListLayout", {
-- ...
}),
Roact.createFragment(list.array)
})
})
end
useAsync
promiseHooks.
useAsync
(
asyncCallback:Â
(
)
 →Â
Promise
<
T
>
) →Â
HookCreator
<
UseAsync
<
T
>
>
Types
interface
UseAsync<T> {
isLoading:Â
boolean
isCancelled:Â
boolean
error:Â
T
 |Â
nil
result:Â
T
 |Â
nil
}
Handles async operations and prevents race conditions.
local function fetchData()
return Promise.new(function()
return httpRequest("http://url...")
end)
end
local function HookedComponent(props, hooks)
local async = useAsync(fetchData)(hooks)
return Roact.createFragment({
Loading = async.isLoading and Roact.createElement(...),
Error = async.error and Roact.createElement(...),
Result = async.result and Roact.createElement(...)
})
end
useCounter
Types
type
UseCounter =
(
number
,
(
)
 →Â
void
,
(
)
 →Â
void
,
(
)
 →Â
void
)
State hook that tracks a numeric value. If no initial value is passed, it will default to 0.
tip
Counters can be increased/decreased by amount if you pass a number to it's function.
local function HookedComponent(props, hooks)
local count, increment, decrement, reset = useCounter()(hooks)
return Roact.createFragment({
Counter = Roact.createElement(MyCounter, {
Text = tostring(count)
}),
Inc = Roact.createElement(Button, {
OnClick = function()
increment()
end
}),
Dec = Roact.createElement(Button, {
OnClick = function()
decrement()
end
}),
Reset = Roact.createElement(Button, {
OnClick = function()
reset()
end
}),
})
end
useDebounce
promiseHooks.
useDebounce
(
time:Â
number
,
callback:Â
(
)
 →Â
void
,
dependencies:Â
{
any
}
) →Â
HookCreator
<
(
(
)
 →Â
boolean
,
(
)
 →Â
void
)
>
Hook that delays invoking a function until after wait seconds have elapsed since the last time the debounced function was invoked.
The third argument is the array of values that the debounce depends on, in the same manner as useEffect
.
The debounce timeout will start when one of the values changes.
tip
You can reach the same result using the hook below.
local function HookedComponent(props, hooks)
local text, setText = hooks.useState("")
local debouncedText, setDebouncedText = hooks.useState(text)
-- Will change debouncedText and render after 1 second of inactivity
useDebounce(1, function()
setDebouncedText(text)
end, { text })(hooks)
return Roact.createFragment({
Label = Roact.createElement(Label, {
Text = debouncedText
}),
Box = Roact.createElement("TextBox", {
-- ...,
[Roact.Change.Text] = function(rbx)
setText(rbx.Text)
end
}),
})
end
useDebouncedText
promiseHooks.
useDebouncedText
(
time:Â
number
,
text:Â
string
) →Â
HookCreator
<
string
>
A shortcut for using useDebounce with a string.
local function HookedComponent(props, hooks)
local text, setText = hooks.useState("")
local debouncedText = useDebouncedText(1, text)(hooks)
return Roact.createFragment({
Label = Roact.createElement(Label, {
Text = debouncedText
}),
Box = Roact.createElement("TextBox", {
-- ...,
[Roact.Change.Text] = function(rbx)
setText(rbx.Text)
end
}),
})
end
useDispatch
roact-roduxroduxHooks.
useDispatch
(
hooks:Â
RoactHooks
) →Â
(
)
 →Â
void
Returns a function that executes the dispatch
method of the store.
You may use it to dispatch actions as needed.
local function Counter(props, hooks)
local dispatch = useDispatch(hooks)
return Roact.createFragment({
Label = Roact.createElement("TextLabel", {
Text = props.value
}),
Increment = Roact.createElement(Button, {
OnClick = function()
dispatch({ type = "increment" })
end
})
})
end
useFlipper
flipperTypes
type
UseFlipper =
(
RoactBinding
<
number
>
,
FlipperMotor
)
Helper hook that takes a flipper motor, connects it to a binding and returns both.
useForceUpdate
Hooks.
useForceUpdate
(
hooks:Â
RoactHooks
) →Â
(
)
 →Â
void
Returns a callback that once called, will cause the component to re-render.
useLatest
Hooks.
useLatest
(
value:Â
T
) →Â
HookCreator
<
{
value:Â
T
}
>
Hook that can be used to return the latest state.
tip
This is useful to get access to the latest version of a value, instead of getting the version from the time an asynchronous callback was created, for example.
local function Demo(props, hooks)
local count, increment = useCounter()(hooks)
local latestCount = useLatest(count)(hooks)
useTimeout(5, function()
print(("Latest count is: %s"):format(latestCount.value))
end)(hooks)
return Roact.createFragment({
Label = Roact.createElement(Label, {
Text = tostring(count)
}),
Button = Roact.createElement(Button, {
OnClick = function()
increment()
end
})
})
end
useMaid
maidHooks.
useMaid
(
maid:Â
Maid
,
dependencies:Â
{
any
}
) →Â
HookCreator
<
void
>
Will clear the maid after one of its dependencies change.
TODO EXAMPLE
usePortal
roactTypes
interface
PortalOptions {
Target:Â
Instance
DefaultShow:Â
boolean
 |Â
nil
DisplayName:Â
string
 |Â
nil
DisplayOrder:Â
number
 |Â
nil
IgnoreGuiInset:Â
boolean
 |Â
nil
OnShow:Â
(
(
)
 →Â
void
)
 |Â
nil
OnHide:Â
(
(
)
 →Â
void
)
 |Â
nil
OnClickOutside:Â
(
(
hide:Â
(
)
 →Â
void
)
 →Â
void
)
 |Â
nil
}
interface
UsePortal {
Portal:Â
Roact.FunctionComponent
isShow:Â
boolean
show:Â
(
)
 →Â
void
hide:Â
(
)
 →Â
void
toggle:Â
(
)
 →Â
void
}
This helps you render children into an element that exists outside the hierarchy of the parent component.
TODO EXAMPLE
usePrevious
Hooks.
usePrevious
(
state:Â
S
) →Â
HookCreator
<
S
>
Simply returns the previous state.
useQueue
Hooks.
useQueue
(
initialValue:Â
{
T
}
) →Â
HookCreator
<
UseQueue
<
T
>
>
Types
interface
UseQueue<T> {
add:Â
(
element:Â
T
)
 →Â
void
remove:Â
(
)
 →Â
void
first:Â
T
 |Â
nil
last:Â
T
 |Â
nil
size:Â
number
}
State hook that implements a simple FIFO queue.
useReactiveState
Hooks.
useReactiveState
(
initialState:Â
T
) →Â
HookCreator
<
T
>
Types
type
HookCreator<T> =
(
hooks:Â
RoactHooks
)
 →Â
T
Creates a state object that can be changed whenever a new value is assigned to a key.
useRendersSpy
Hooks.
useRendersSpy
(
hooks:Â
RoactHooks
) →Â
number
Returns the amount of renders the component has since its mount.
useSelector
roact-roduxroduxHooks.
useSelector
(
selector:Â
(
state:Â
S
)
 →Â
R
,
equalityFn:Â
(
(
a:Â
any
,
b:Â
any
)
 →Â
boolean
)
 |Â
nil
) →Â
HookCreator
<
R
>
Allows you to extract data from the store state, using a selector function.
warning
Selectors should be pure since they are potentially executed multiple times and at arbitrary points in time.
useStore
roact-roduxroduxHooks.
useStore
(
hooks:Â
RoactHooks
) →Â
Store
Returns a reference to the same store that was passed in to the StoreProvider
component.
caution
This should not be used frequently. If you just want to retrieve data from the store, prefer useSelector instead.
useTimeout
promiseHooks.
useTimeout
(
time:Â
number
,
callback:Â
(
)
 →Â
void
,
onCancel:Â
(
(
timeLeft:Â
number
)
 →Â
void
)
 |Â
nil
) →Â
HookCreator
<
UseTimeout
>
Types
interface
UseTimeout {
cancel:Â
(
)
 →Â
void
reset:Â
(
)
 →Â
void
}
Re-renders the component after a specified number of seconds. Provides handles to cancel and/or reset the timeout.
info
This is a one time call. Once the timeout is finished, you cannot use it again. If you are looking into something that can change accordingly to its dependencies, check useDebounce.
local function HookedComponent(props, hooks)
local text, setText = useState("")
local timeout = useTimeout(3, function()
setText("This took 3 seconds!")
end)(hooks)
return Roact.createElement("TextLabel", {
-- ...
Text = text,
})
end
useToggle
Types
type
UseToggle =
(
boolean
,
(
value:Â
boolean
 |Â
nil
)
 →Â
void
)
State hook that tracks value of a boolean.
useTween
Types
interface
UseTween {
play:Â
(
)
 →Â
void
pause:Â
(
)
 →Â
void
cancel:Â
(
)
 →Â
void
onCompleted:Â
(
callback:Â
(
playbackState:Â
Enum.PlaybackState
)
 →Â
void
)
 →Â
void
}
Takes a TweenInfo class and returns a binding and an object to manage the tweening.
TODO EXAMPLE
useUndo
Hooks.
useUndo
(
initialPresent:Â
T
) →Â
HookCreator
<
(
UseUndoState
<
T
>
,
UseUndo
<
T
>
)
>
Types
interface
UseUndo<T> {
set:Â
(
newPresent:Â
T
)
 →Â
void
reset:Â
(
newPresent:Â
T
)
 →Â
void
undo:Â
(
)
 →Â
void
redo:Â
(
)
 →Â
void
canUndo:Â
boolean
canRedo:Â
boolean
}
interface
UseUndoState<T> {
past:Â
{
T
}
present:Â
T
future:Â
{
T
}
}
Stores defined amount of previous state values and provides handles to travel through them.
TODO EXAMPLE
useUpdateEffect
Hooks.
useUpdateEffect
(
callback:Â
(
)
 →Â
(
(
)
 →Â
void
)
 |Â
void
,
dependencies:Â
{
any
}
 |Â
nil
) →Â
HookCreator
<
void
>
Does the exactly same thing useEffect
do, but ignores the first render.