Hi, we are porting a bit of React code to Reason, and ran into this question: How does one get props from children in ReasonReact?
For example this bit of React code iterates over this.props.children, and uses object destructuring to pull out the label out of props, which is used as a tooltip for each Tab:
// React class method
render() {
const tabElements = Children.toArray(this.props.children);
return (
<>
<TabBar>
{tabElements.map(({props: {label}}, i) => (
<Tab
key={i}
data-tip={label} // <----- Here
data-for={TOOLTIP_ID}
...
>
...
</Tab>
))}
<Tooltip id={TOOLTIP_ID} effect="solid" delayShow={500} place="bottom" />
</TabBar>
{tabElements[this.state.selectedTabIndex]}
</>
);
}
When we ported to Reason, the developer had to use bs.raw to extract the props. There are other problems with this Reason code too, like using a string for the pattern match, instead of using a variant type. But the bs.raw usage seems to be really the crux of it:
let getLabel: ReasonReact.reactElement => string =
[%bs.raw "reactElement => reactElement.props.label"];
let getTabId: ReasonReact.reactElement => string =
[%bs.raw "reactElement => reactElement.props.tabId"];
[@react.component]
let make = (~children: array(ReasonReact.reactElement)) => {
let (selectedTabIndex, setSelectedTabIndex) = React.useState(_ => 0);
<>
<TabBar>
(ReasonReact.array(
children |> Array.mapi((i, tabElement) =>
<Tab
key=string_of_int(i)
dataFor=tabButtonTooltipId
dataTip=getLabel(tabElement) /* <---- here */
>
(
switch (getTabId(tabElement)) { /* <---- here */
| "data" => <OpenIcon height="20px" />
| "layer" => <LayerIcon height="20px" />
| "filter" => <FilterIcon height="20px" />
| "interaction" => <InteractionIcon height="20px" />
| "map" => <SettingsIcon height="20px" />
| _ => ReasonReact.null
}
)
</Tab>
)
))
<Tooltip id=tabButtonTooltipId effect="solid" delayShow=500 place="bottom" />
</TabBar>
(Array.get(children, selectedTabIndex))
</>
};
Any suggestions or guidance appreciated! We are ReasonML newbies. Thanks