I'm learning reasonml and quite excited about it. Something I often do in typescript react code is:
type Props = React.HTMLProps<HTMLButtonElement> & { foo: boolean }
const SuperButton: React.FC<Props> = (props) => <button {/* stuff with props */ />
In this regard, I communicate to my users as a component library provider that this button extends normal HTML button attributes.
How can I express and extend normal html component attributes in my components?
I see that reason explicitly doesn't support spreading props: https://github.com/reasonml/reason-react/blob/master/docs/props-spread.md.
I do see that there is a composition strategy: How to compose props across component in reason-react bindings?, but not sure how to marry that up with normal HTML element component stuffs.
Any recommendations? Thanks!
It's possible to do something similar using
ReasonReact.cloneElement
, as Amirali hinted. The idea is to split up your component's props and the HTML button's props into two separate parameters for your component, render your button, and then clone it while also injecting the extra button props.This page shows a component which encapsulates this clone-and-injection functionality:
Now, you can use this
Spread
component for yourSuperButton
component:The
htmlButtonProps
prop will contain the regular HTML button props, while separatelyfoo
is your component's specific prop. The component can be used like this:Small housekeeping note: you don't actually need to define the modules with the
module
keyword. If you want you can put them in separate files calledSpread.re
andSuperButton.re
. Reason files automatically become modules.