Misc
Optional Props
// Greeting.res
@react.component
let make = (~name: option<string>=?) => {
let greeting = switch name {
| Some(name) => "Hello " ++ name ++ "!"
| None => "Hello stranger!"
}
<div> {React.string(greeting)} </div>
}
let name = Some("Andrea")
<Greeting ?name /> // <== just ?name
Special Props key and ref
keyandrefare special props, cannot define them like~keyor~ref
Invalid Prop Names
- Reserved keywords like
typeshould be used liketype_ aria-*should be likeariaLabeldata-attributes can be added usingReact.cloneElement
Children
children is treated as a React.Element.
Component that must have children
module Comp = {
@react.component
let make = (~children: React.element) => { ...... }
}
Component that doesn't have children
module Comp = {
@react.component
let make = () => { ...... }
}
Component with optional children
module Comp = {
@react.component
let make = (~children: option<React.element>=?) => { ...... }
}
Component Naming
// File.res
// will be named `File` in dev tools
@react.component
let make = ...
// will be named `File$component` in dev tools
@react.component
let component = ...
module Nested = {
// will be named `File$Nested` in dev tools
@react.component
let make = ...
};