-
Notifications
You must be signed in to change notification settings - Fork 65
Add support for createRef
#167
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Thanks for looking into the |
After looking into this over the last couple of days, I'm struggling to find a pleasant implementation that supports both the new Problem summaryThere are two ways to create refs ( myClass :: React.ReactClass {}
myClass = React.component "MyClass" \this -> do
domRef <- Ref.createDOMRef
instanceRef <- Ref.createInstanceRef
pure
{ state: { domRef, instanceRef }
, render: render this <$> React.getState this
}
where
render this { domRef, instanceRef }
= React.DOM.div
[]
-- 1. DOM ref using createRef
[ React.DOM.input
[ Props._type "text"
, Props.ref domRef
]
-- 2. DOM ref using callback
, React.DOM.div
[ Props.ref \ref -> do
-- Do something here
]
[ ]
-- 3. Instance ref using createRef
, React.createLeafElement anotherClass
{ ref: instanceRef
}
-- 4. Instance ref using callback
, React.createLeafElement anotherClass
{ ref: \ref -> do
-- Do something here
}
] The challenge therefore is to find a nice API that works in all these cases. It's also worth noting that in React you get slightly different results depending on how you create a ref: with Attempted approaches
|
In my local projects when dealing with FFI overloading like this, I usually use an opaque foreign data type with smart constructors that are coercions under the hood. Maybe something like: foreign import data RefHandler :: Type -> Type
foreign import fromEffect :: forall a. (a -> Effect Unit) -> RefHandler a
foreign import fromEffectFn :: (EffectFn1 a Unit) -> RefHandler a
foreign import fromRef :: Ref a ->RefHandler a |
@elliotdavies Thanks for working out the above problem summary and proposed solutions. It is very much appreciated. I am inclined to agree with @natefaubion that using the opaque foreign type along with functions for the appropriate coercions is a good way to go. Would you be open to trying something like this? |
@natefaubion @ethul Cheers for getting back to me. This approach sounds like it should work - I'll certainly give it a go. I'm away for the next three weeks but I'll see about it once I'm back 😄 |
@natefaubion @ethul Sorry for the lengthy delay! I finally got round to this and have submitted a PR for discussion. |
I was doing some exploratory work around adding support for React's
createRef
API when I noticed a lot of similar work had already been done as part of the hooks PR: https://github.com/purescript-contrib/purescript-react/pull/159/files#diff-d96e307e87fe77fec439e08ec13d4947I'm not sure what the status of that PR is, but how do we feel about introducing the
createRef
part of it separately? (Probably including the newRef a
type, the accompanying functions, and theForwardRef
stuff.) I'd certainly be keen to help with this.The text was updated successfully, but these errors were encountered: