[RFC - React ppx v4] Support the React.forwardRef

There are two different ways to forward ref as per the doc.

  1. Forward ref via props Forwarding Refs | React
  2. [Discouraged] React.forwardRef Forwarding Refs | React

First of all, I’d like to have your thought about the discouraged usage against the first one. Secondly, do you have special cases or pain points you should use the discouraged usage instead forwarding via props?

Background:

The discouraged usage makes the react ppx v4 has unnecessary complexities, so ppx v4 suggests more cleaner expression in spite of using React.forwardRef.

@react.component
let make = (~name) => {
  let _ = ()
  ref_ => {
    <input name ref=?{Js.Nullable.toOption(ref_)->Belt.Option.map(ReactDOM.Ref.domRef)} />
  }
}
let make = React.forwardRef(make)

Just to clear up some ambiguity: forwardRef needs to be supported to allow interop with existing JS libraries and to be able to write your component libraries in ReScript with the intention for consuming in JS or TS - in those languages it’s much more expected to be able to pass a ref prop.

Tbh I think the discouragement of forwardRef is wrong, it doesn’t align with the goals of ReScript and rescript-react of being intuitive to pick up if you’re a JS developer.

For that reason, I would vote for being able to keep the let make = forwardRef syntax and also vote to update the docs to not discourage forwardRef.

2 Likes

Thank you for your opinion. I have to admit having some struggle and racking my brain to implement. I’ve fixed the ppx and get it done for React.forwardRef. Let’s see how the votes are going anyway.