createRef
createRef declares a ref at module scope: a name for one component in one
interface. Bound to a <ui> it names the interface; bound to a static
element it names the slot; bound anywhere else it is a ref like any other, and
one that can be exported and handed to a component as a constant.
createRef(name?: string): RefReference
Section titled “Reference”createRef(name)
Section titled “createRef(name)”const plate = createRef('docs-marker:plate'); <layer ref={plate} x="left" y="center" w={PLATE} h={PLATE}Parameters
Section titled “Parameters”name: A string. The durable name. On a<ui>it is the interface’s name; on astaticelement it is the slot’s. Optional — a ref that nothing outside the build addresses needs none.
Returns
Section titled “Returns”A Ref, with every field useRef’s
has. It is bound the same way, once, with ref={…}.
Caveats
Section titled “Caveats”- One name for one component. Bound twice, anywhere in the project, it is
refused (
TS2E214). For a component drawn several times — a mapped row — useuseRef(), which is one handle per instance. - It travels as a constant. A
useRef()handle cannot leave the render that made it; acreateRefcan be exported, imported, and handed to a component as an ordinary prop. - A
<ui>with norefhas an id and no name, and nothing outside the build can open it.
const label = createRef("docs-refused-label");
const Refused = (): Component => ( <layer w="fill" h="fill"> <text ref={label} x={0} y={0} w="fill" h={16} text="one" font={Fonts.PLAIN} color={Colors.AMBER} /> <text ref={label} x={0} y={20} w="fill" h={16} text="two" font={Fonts.PLAIN} color={Colors.AMBER} /> </layer>);Naming an interface
Section titled “Naming an interface”const shopRef = createRef('docs-shop');
export const shopUi = (): UI => ( <ui ref={shopRef} w={252} h={260}>Naming a slot
Section titled “Naming a slot”export const walletSlot = createRef('docs-integrating:wallet'); <layer static ref={walletSlot} x={12} y={44} w={88} h={126}>Handing a ref to a component
Section titled “Handing a ref to a component” <MoveButton label="Left" mode={0} x={0} target={plate} readout={readout} />MoveButton declares target: Ref and writes through it.
Hosting a watch on a named component
Section titled “Hosting a watch on a named component”const poisonLabel = createRef('docs-watch:poison'); useWatch(Varps.POISON, (event) => { writePoison(event.component); }, poisonLabel);Troubleshooting
Section titled “Troubleshooting”Which one do I want, useRef or createRef?
Section titled “Which one do I want, useRef or createRef?”useRef() inside a component that may be drawn several times; createRef
when there is exactly one of the thing, or when the name has to be known
outside the build.
My ref is already bound to another element
Section titled “My ref is already bound to another element”A module-scope ref names one component. If two elements need handles, declare two refs.