-
Notifications
You must be signed in to change notification settings - Fork 65
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
Add support for createRef
API
#172
Conversation
Thanks for your PR! I will have to dig into this, might take me a bit of time. Apologies for that, but I will get to it. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very nice work, just one small bug
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me, just a quick note on the getCurrentRef_
impl.
src/React/DOM/Props.purs
Outdated
@@ -20,6 +19,7 @@ import React.SyntheticEvent | |||
, SyntheticUIEvent | |||
, SyntheticWheelEvent | |||
) | |||
import Web.HTML.HTMLElement (HTMLElement) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a quick note. This is coming from purescript-web-html
, but I didn't notice the dependency in the bower file. Is it being pulled in from another dependency?
On this note, I am unsure if we want this as a dependency since runtimes like react native, etc., would not necessarily require it. So far we have been attempting to keep dependencies at a minimum. That being said, I am not completely opposed to adding the dependency, but wanted to open it up for discussion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ethul Well spotted; I think I forgot to push the change to the bower file. I had added "purescript-web-html": "^2.2.0"
locally.
I suppose that we could have a local HTMLElement
type here and not use the library version. It would save on the dependency, but on the other hand anyone using DOM refs would have to write a conversion function (i.e. unsafeCoerce
), which might be annoying.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for adding the dependency.
src/React/DOM/Props.purs
Outdated
@@ -894,8 +894,8 @@ onScrollCapture f = unsafeMkProps "onScrollCapture" (mkEffectFn1 f) | |||
onWheelCapture :: (SyntheticWheelEvent -> Effect Unit) -> Props | |||
onWheelCapture f = unsafeMkProps "onWheelCapture" (mkEffectFn1 f) | |||
|
|||
ref :: (Nullable ReactRef -> Effect Unit) -> Props | |||
ref f = unsafeMkProps "ref" (mkEffectFn1 f) | |||
ref :: Ref.RefHandler HTMLElement -> Props |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this always HTMLElement
here? For example, on React Native, would this be something different?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good question. I haven't used React Native myself, but I looked into this and as far as I can tell Native doesn't have refs - it seems to use something called Direct Manipulation instead. This does raise the question of why the React team put refs in the React library instead of ReactDOM; I'm not sure what the answer is.
Thanks for looking into this. You’re right that react native uses direct
manipulation, but I believe one would still need to use the ref prop to do
this. From the link:
<View ref={component => this._root = component} {...this.props}>
…On Fri, Aug 2, 2019 at 10:13 Elliot Davies ***@***.***> wrote:
***@***.**** commented on this pull request.
------------------------------
In src/React/DOM/Props.purs
<#172 (comment)>
:
> @@ -894,8 +894,8 @@ onScrollCapture f = unsafeMkProps "onScrollCapture" (mkEffectFn1 f)
onWheelCapture :: (SyntheticWheelEvent -> Effect Unit) -> Props
onWheelCapture f = unsafeMkProps "onWheelCapture" (mkEffectFn1 f)
-ref :: (Nullable ReactRef -> Effect Unit) -> Props
-ref f = unsafeMkProps "ref" (mkEffectFn1 f)
+ref :: Ref.RefHandler HTMLElement -> Props
Good question. I haven't used React Native myself, but I looked into this
and as far as I can tell Native doesn't have refs - it seems to use
something called Direct Manipulation
<https://facebook.github.io/react-native/docs/direct-manipulation>
instead. This does raise the question of why the React team put refs in the
React library instead of ReactDOM; I'm not sure what the answer is.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#172?email_source=notifications&email_token=AACVRSZVDQDALMMWD7MOU73QCQ6JJA5CNFSM4H4UYBH2YY3PNVWWK3TUL52HS4DFWFIHK3DMKJSXC5LFON2FEZLWNFSXPKTDN5WW2ZLOORPWSZGOCANPRDI#discussion_r310150245>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AACVRS2GSDCD4VVT4Q2IXBTQCQ6JJANCNFSM4H4UYBHQ>
.
|
@ethul Hmm, after a closer reading of that docs page I think you're correct. I would guess that
and let the user coerce it as they see fit? This would also avoid the |
I suppose I am kind of leaning in that direction. However, I am open to
further discussion on the matter. If we go this route, maybe the name could
be NativeNode or ReactNode, or something like that? Thanks again for
working on this.
…On Fri, Aug 2, 2019 at 10:46 Elliot Davies ***@***.***> wrote:
@ethul <https://github.com/ethul> Hmm, after a closer reading of that
docs page I think you're correct. I would guess that ReactInstance is
correct across web/native for a ref on a custom component, but presumably
you get either a DOM node or some sort of native element node if you use a
ref on a web/native primitive. In that case do you think it's best to have
something like
ref :: Ref.RefHandler PrimitiveNode -> Props
and let the user coerce it as they see fit? This would also avoid the
web-html dependency. (Open to suggestions for a decent type name here!)
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#172?email_source=notifications&email_token=AACVRS5FFLE5WXNHJZAA4GLQCRCFFA5CNFSM4H4UYBH2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD3N6QQY#issuecomment-517728323>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AACVRS26MAITFKBP4LZGKW3QCRCFFANCNFSM4H4UYBHQ>
.
|
@LiamGoodacre I've updated the FFI implementation such that callback refs are lifted into an object like @ethul I've updated the types to use |
Thanks so much for making these updates. Looking great! |
@ethul No problem! Anything else you'd like doing to this before we can get it merged? |
Thanks! All looks good to me |
Fixes #167 by adding support for React's
createRef
API. Also distinguishes between DOM refs and React instance refs.I've created a branch of
purescript-react-example
to showcase the different combinations of props, callbacks and ref types (see here in particular).The
ReactInstance
type probably isn't in the right place; I can move it somewhere else. (React.purs
feels better but that causes an import cycle.)Otherwise the only thing I dislike is having to checkref.current
to see whether the item is a ref, or an object with the shape{ current: <ref> }
(see https://reactjs.org/docs/refs-and-the-dom.html#accessing-refs). I'm not sure I see a way around this, though.