I’m writing bindings to the nativewind
styled
function which adds a className
prop to react native components. I’ve managed to create local components and pass them through the external styled function and everything generally renders as expected, but the final piece is getting a usable className
prop on each new local component. Here’s what I have working so far:
@module("nativewind")
external setStyled: React.componentLike<'props, 'return> => React.componentLike<'props, 'return> =
"styled"
module View = {
let makeProps = ReactNative.View.makeProps
let make = ReactNative.View.make->setStyled
}
module Text = {
let makeProps = ReactNative.Text.makeProps
let make = ReactNative.Text.make->setStyled
}
So that works (renders) but again doesn’t reflect the className prop which was added as of the call to setStyled
. In my perfect world, I’d somehow be able to do this at the point of binding:
@module("nativewind")
external setStyled: React.componentLike<'props, 'return> => React.componentLike<
{
...'props,
"className": string /* This doesn't even come close to working, but it's effectively what I need */
}, 'return> =
"styled"
So assuming I can’t do anything like the last idea, what it seems like I should do is something like the following in a second wrapping step:
module Wrapper = {
let make = (~props, ~children, ~className) => {
React.cloneElement(Text.make(/*this wouldn't work*/~props, ~children), {"className": className})
}
}
But that doesn’t work either. So I guess my question is: what’s the best point of intercept to add a prop to a component in a flexible way that preserves other props, children, etc.?