Hooks
A collection of hooks made for React.
Functions
useAsync
State ManagementHooks.
useAsync
(
promiseOrGetPromise:
Promise
<
T
>
|
(
)
→
Promise
<
T
>
,
--
The Promise to wrap.
dependencies:
{
unknown
}
?
) →
(
T?
,
--
The result of the Promise.
unknown?
,
--
The error message of the Promise.
Promise.Status
--
The status of the Promise.
)
useAsync
wraps a Promise to a state.
info
Taken from here
useClickOutside
This item only works when running on the client. ClientUser InterfaceHooks.
useClickOutside
(
onClick:
(
)
→
(
)
,
--
A callback to call when a click or touch event is detected outside of the element.
userInputTypes?:
{
Enum.UserInputType
}
,
--
A list of user input types to detect. Defaults to Enum.UserInputType.MouseButton1
and Enum.UserInputType.Touch
.
guiObjects?:
{
GuiObject
}
--
A list of elements to detect clicks outside of. Defaults to the element returned by useRef
.
) →
Ref
<
GuiObject
>
--
A ref to the element to detect clicks outside of.
A hook used to detect click and touch events outside of specified element.
Luau:
local function Component()
local visible, setVisible = React.useBinding(false)
local ref = Hooks.useClickOutside(function()
setVisible(false)
end)
return React.createElement("Frame", {
AnchorPoint = Vector2.new(0.5, 0.5),
BackgroundColor3 = Color3.fromRGB(255, 255, 255),
BorderSizePixel = 0,
Position = UDim2.fromScale(0.5, 0.5),
Size = UDim2.fromOffset(100, 100),
Visible = visible,
ref = ref,
})
end
useCurrentCamera
This item only works when running on the client. ClientHooks.
useCurrentCamera
(
onChange?:
(
currentCamera:
Camera
)
→
(
)
--
A callback that is called when the current camera changes.
) →
Camera
--
The current camera.
A hook used for getting the current camera.
useDelayedValue
State ManagementHooks.
useDelayedValue
(
value:
T
,
--
The value to delay.
delayInSeconds:
number
--
The amount of time to delay the value by.
) →
T
--
The delayed value.
A hook used for delaying a value.
useExternalEvent
LifecycleHooks.
useExternalEvent
(
callback:
(
...any
)
→
(
)
--
The callback to connect to the event.
) →
(
)
A hook that connects and disconnects a callback to an event. This is really
just a fancy useEffect
wrapper.
Luau:
local function Component()
end
useForceUpdate
LifecycleHooks.
useForceUpdate
(
) →
(
)
→
(
)
--
The function to call to force a re-render.
A hook that creates a function used to force a component to re-render.
useId
State ManagementHooks.
useId
(
staticId?:
string
--
A static id to use instead of a generated one.
) →
string
--
The generated id.
The useId
hook generates random id that persists across renders. Hook is usually used to bind input elements to labels. Generated random id is saved to ref and will not change unless component is unmounted.
useMemoWithCleanup
State ManagementHooks.
useMemoWithCleanup
(
createValue:
(
)
→
T
,
--
The function to create the value.
cleanupValue:
(
value:
T
)
→
(
)
,
--
The function to clean up the value.
dependencies?:
{
unknown
}
--
The dependencies to check for changes.
) →
T
--
The memoized value.
A hook that memoizes a value and cleans it up when the dependencies change.
usePrevious
State ManagementHooks.
usePrevious
(
value:
T
--
The value to store.
) →
T?
--
The previous value of the state.
The usePrevious
hook stores the previous value of a state in a ref, it returns undefined on initial render and the previous value of a state after rerender.
useRainbowColorSequence
UIEffectsHooks.
useRainbowColorSequence
(
parameters?:
IParameters
--
The parameters for the hook.
) →
Binding
<
ColorSequence
>
--
The rainbow color sequence.
An example hook that uses a rainbow color sequence.
useRendersSpy
LifecycleDebuggingHooks.
useRendersSpy
(
) →
number
--
The amount of renders.
Returns the amount of times the component has been rendered since mounting. This is a very useful hook for testing.
useScale
This item only works when running on the client. ClientUIHooks.
useScale
(
scale:
number
,
--
The scale to use for the UI element.
) →
Binding
<
number
>
--
The scale to use for the UI element.
A hook used for scaling UI elements to fit the current screen.
useShallowEffect
LifecycleHooks.
useShallowEffect
(
callback:
(
)
→
(
)
,
--
The callback to run when the dependencies change.
dependencies?:
{
unknown
}
--
The dependencies to compare.
) →
(
)
useEffect
drop in replacement with dependencies shallow comparison. useShallowEffect
works
exactly like useEffect
but performs shallow dependencies comparison instead of referential comparison.
useStrokeScale
This item only works when running on the client. ClientUIEffectsHooks.
useStrokeScale
(
pixelThickness?:
number
,
--
The thickness of the stroke in pixels. Defaults to 1.
relativeSize?:
number
--
The relative size of the screen. Defaults to 985.
) →
Binding
<
number
>
--
The thickness of the stroke in pixels.
A hook used to scale a stroke size based on the viewport size.
useSyncExternalStore
PolyfillHooks.
useSyncExternalStore
(
subscribe:
(
onStoreChange:
(
)
→
(
)
)
→
(
)
→
(
)
,
--
A function that subscribes to the external store.
getSnapshot:
(
)
→
Snapshot
--
A function that returns the current snapshot of the external store.
) →
Snapshot
--
The current snapshot of the external store.
A polyfill for React 18's useSyncExternalStore
hook.
info
For more info, check out the issue on the React 18 Working Group.
useSyncExternalStoreWithSelector
PolyfillHooks.
useSyncExternalStoreWithSelector
(
subscribe:
(
onStoreChange:
(
)
→
(
)
)
→
(
)
→
(
)
,
--
A function that subscribes to the external store.
getSnapshot:
(
)
→
Snapshot
,
--
A function that returns the current snapshot of the external store.
selector:
(
snapshot:
Snapshot
)
→
Selected
,
--
A selector function that is used to select a value from the snapshot.
isEqual?:
(
left:
Selected
,
right:
Selected
)
→
boolean
--
A function used to determine if the selected value has changed.
) →
Selected
--
The selected value for the current snapshot of the external store.
A polyfill for React 18's useSyncExternalStore
hook that allows for a selector.
info
For more info, check out the issue on the React 18 Working Group.
useTheme
This item only works when running in the context of a plugin. PluginA hook that returns the current theme. Used for Plugins.
useUncontrolled
State ManagementHooks.
useUncontrolled
(
input?:
IUseUncontrolledInput
<
T
>
--
The input to use.
) →
(
T
,
--
The value to use.
(
value:
T
)
→
(
)
,
--
The onChange handler to use.
boolean
--
Whether the component is controlled or not.
)
Manage state of both controlled and uncontrolled components.
useViewportSize
This item only works when running on the client. ClientA hook that returns the current ViewportSize of the CurrentCamera.
useWhyDidYouUpdate
DebugHooks.
useWhyDidYouUpdate
(
name:
string
,
--
The name of the component.
props:
table
--
The props of the component.
) →
(
)
Quickly see which prop changed and caused a re-render by adding a single line to the component.