ReactObservableValue
React hooks for ObservableValue: read a value and rerender, select part of it, or follow it with a binding.
Functions
useObservableValue
ReactObservableValue.useObservableValue(source: ObservableValue<T>) → (T,--
the current value
number--
increases every time the value changes
)
Returns the ObservableValue’s current value, and rerenders the component whenever it changes. The second return value
increases on every change so it can be used as a useMemo / useEffect dependency even when a table was
changed in place.
local function HealthBar(props: { health: ObservableValue.ObservableValue<number> })
local health = ReactObservableValue.useObservableValue(props.health)
return React.createElement("TextLabel", { Text = `{health} HP` })
end
useObservableBinding
ReactObservableValue.useObservableBinding(source: ObservableValue<T>) → Binding<T>
Returns a React binding that follows the ObservableValue without rerendering the component. Bindings update
instance properties directly, which suits values that change often, like progress bars and timers. Use the
binding's map to turn the value into a property.
local function ProgressBar(props: { progress: ObservableValue.ObservableValue<number> })
local progress = ReactObservableValue.useObservableBinding(props.progress)
return React.createElement("Frame", {
Size = progress:map(function(value)
return UDim2.fromScale(value, 1)
end),
})
end
useObservableSelector
ReactObservableValue.useObservableSelector(source: ObservableValue<T>,selector: (value: T) → U,isEqual: ((previous: U,current: U) → boolean)?) → U
Returns selector(value) and rerenders the component only when that result changes, not on every change to the
ObservableValue. Use it to read part of a table, or something computed from the value.
Results are compared with == unless you pass isEqual. A selector that builds a new table each time needs an
isEqual, or it rerenders on every change.
local function BlurOverlay(props: { blurStack: ObservableValue.ObservableValue<{ [number]: true }> })
local isVisible = ReactObservableValue.useObservableSelector(props.blurStack, function(stack)
return next(stack) ~= nil
end)
return React.createElement("Frame", { Visible = isVisible })
end