@dialstack/sdk-dev / react/src / useUpdateWithSetter
Function: useUpdateWithSetter()
function useUpdateWithSetter<T, V>(
component,
value,
onUpdated
): void;
Defined in: sdk/react/src/react/useUpdateWithSetter.ts:50
Hook that synchronizes a React prop value to a Web Component setter method
This enables declarative React patterns (props) to control imperative Web Component APIs (setter methods) in a type-safe way.
The setter runs only when the component or the value changes, compared by identity like React's own effect deps, so a host that re-renders often doesn't make the element re-render or reload. That makes two things part of the contract:
onUpdatedmust depend only oncomponentandvalue. A setter closing over other state isn't re-run when only that state changes; pass it invalueinstead.- Keep object props stable (a constant,
useMemo, state) and replace rather than mutate them: an inline{…}is a new value on every render, and a value mutated in place is the same one.
Type Parameters
T
T extends HTMLElement
V
V
Parameters
component
T | null
The Web Component instance (or null if not mounted)
value
V | undefined
The prop value to sync
onUpdated
(component, value) => void
Callback that receives the component and value, should call the setter
Returns
void
Example
const Voicemails = ({ userId, onVoicemailSelect }) => {
const { containerRef, componentInstance } = useCreateComponent(dialstack, 'voicemails');
// Type-safe: TypeScript knows componentInstance has setUserId method
useUpdateWithSetter(componentInstance, userId, (comp, val) => {
comp.setUserId(val);
});
// A callback: removing the prop clears it on the element.
useUpdateHandlerWithSetter(componentInstance, onVoicemailSelect, (comp, val) => {
comp.setOnVoicemailSelect(val);
});
return <div ref={containerRef} />;
};