Rookv1.0
РусскийRU

Explicit needs

The compiler infers context requirements from reads such as @theme.labelin a component or decorator. Named component tags pass the requirement up the tree until a decorator provides it.

A wrapper that accepts a component value can use needs to declare what context its callers must provide, even when the concrete component is selected at runtime. Here Slot requires @theme.

dec theme(public label string) {}

comp Caption() {
  return <span>{@theme.label}</span>
}

comp Slot(View comp()) needs theme {
  return <View />
}

@theme('Settings')
comp App() {
  return <Slot View={Caption} />
}

document.body.append(<App />)

The clause adds to the inferred requirements. Write it after the parameters or the server component’s result type, and before includes in a decorator. Use commas to list multiple decorators. The compiler checks named component tags, and a missing provider in a call through a component value raises a runtime error.