-
Notifications
You must be signed in to change notification settings - Fork 65
refs #100
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
refs #100
Conversation
I think this means adding a dependency on |
The other option is to use |
|
I think this looks good. I want to take a bit closer look when I am at my
computer. However, one question is regarding readRefImpl. Wouldn't this
return undefined instead of null? There is a purescript-undefinable module.
Thanks for working on this!
…On Sat, Jul 8, 2017 at 08:17 Coot ***@***.***> wrote:
- now it only introduces purescript-dom dependency, which anyway will
be in any project
- I think we could be less conservative and use HTMLElement instead of
Node type. Storing an attribute node (which is a Node but not
Element), is possible but not something usable.
- we could be more conservative on effects that we allow in refCb, for
example (refs :: ReactRef (write :: Write | access), state ::
ReactState () | eff), there is no point on reading the state, and
writting it will actually raise an exception
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#100 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAVYy5w-4Ytto8S6F_NCE57Tl29AHAebks5sL3NcgaJpZM4OHRBs>
.
|
purescript-nullable is using |
Gotcha. Thanks for confirming
…On Sat, Jul 8, 2017 at 08:49 Coot ***@***.***> wrote:
purescript-nullable is using == null to check if something is null or not
and undefined == null returns true. TherefCb is called with null, so it
might be set to null. Undefinable
<https://github.com/ethul/purescript-undefinable/blob/master/src/Data/Undefinable.js#L6>
is using === undefined so this will not detect a null. So Nullable looks
safer to me.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#100 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAVYy4YUP6Ua2sv42-vGpmf6OLp82uA_ks5sL3rhgaJpZM4OHRBs>
.
|
Unfortunately, I'll have a think about some options. |
We could make refs polymorphic in the type. That would leave choice weather use Since this library is oriented towards the web, a requirement of |
After thinking about this further. I agree with @paf31. I think if we can avoid depending on |
src/React/DOM/Props.purs
Outdated
-- | ``` | ||
refCb | ||
:: forall access eff | ||
. (Node -> Eff (refs :: ReactRefs (write :: Write | access) | eff) Unit) |
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, would it make sense for this to be Nullable Node
?
https://facebook.github.io/react/docs/refs-and-the-dom.html#caveats
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.
Yes
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: I am wondering if perhaps refCb
should be called ref
since it is the encourage usage in React. Maybe the current ref
prop can be called refString
or something along those lines. What do you think?
I think it would be very useful to merge in this PR. However, I do think it would be best to avoid the DOM dependency. Would it make sense to replace ref :: forall access eff ref. (Nullable ref -> Eff (refs :: ReactRefs (write :: Write | access) | eff) Unit) |
But wouldn't the user be able to use such a function unsafely? Do we even use the |
Indeed it could be used in an unsafe way. I think a new foreign type would
work.
…On Fri, Sep 1, 2017 at 20:53 Phil Freeman ***@***.***> wrote:
But wouldn't the user be able to use such a function unsafely?
Do we even use the ref type anywhere? What about just creating a new foreign
data type, and then the user can coerce it if they want to pull in dom.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#100 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAVYy1AKFKj1l_XrG5B5Mrw-QemS5gkyks5seKb7gaJpZM4OHRBs>
.
|
Still adds two small dependencies * purescript-maybe * purescript-nullable
src/React.purs
Outdated
@@ -294,6 +300,26 @@ foreign import getRefs :: forall props state access eff. | |||
ReactThis props state -> | |||
Eff (refs :: ReactRefs (read :: Read | access) | eff) Refs | |||
|
|||
foreign import data Ref :: Type |
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.
Could you please add a comment here?
src/React/DOM/Props.purs
Outdated
-- | ``` purescrript | ||
-- | div [ refCb (writeRef this "inputEl") ] [...] | ||
-- | ``` | ||
refCb |
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.
I don't like this name so much, what about something like withRef
?
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.
I actually I don't like it either, withRef
sounds much better :)
src/React.purs
Outdated
String -> | ||
Eff (refs :: ReactRefs (read :: Read | access) | eff) (Nullable Ref) | ||
|
||
readRef :: forall props state access eff. |
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.
Could you please add comments to these as well?
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.
Ref
type should also be documented, and advising the user to use unsafeCoerce
make me feel I am doing something wrong, so either we provide a third library with Node -> Ref
and Ref -> Node
safe functions (purescript-react-refs
) or we can include that in purescript-react-dom
, which already depends on purescript-dom
.
There are pros and cons of both, but either will provide a safe way for the basic operation.
By the way: using zypher & webpack (with uglify plugin) on purescript-isomorphic-react-example, which is pulling DOM for very basic stuff (access to location, history, search the mounting point in the DOM), pulls ~2.3KB of data from purescript-dom
, which is ~0.5% of whole bundle (~400KB, which gzips to 94KB). zypher, to some extend can remove things from foreign modules that's why the overall code from DOM that lands in the bundle is pretty minimal.
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.
In my opinion, I think purescript-react-dom
would be a nice spot for functions that convert between Ref
and Node
. Similarly, in a purescript-react-native
library, there could be functions that convert between Ref
and something native.
src/React/DOM/Props.purs
Outdated
-- | ``` | ||
refCb | ||
:: forall access eff | ||
. (Node -> Eff (refs :: ReactRefs (write :: Write | access) | eff) Unit) |
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: I am wondering if perhaps refCb
should be called ref
since it is the encourage usage in React. Maybe the current ref
prop can be called refString
or something along those lines. What do you think?
src/React/DOM/Props.purs
Outdated
-- | ``` | ||
refCb | ||
:: forall access eff | ||
. (Ref -> Eff (refs :: ReactRefs (write :: Write | access) | eff) Unit) |
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 as a note, I think this may need to be
(Nullable Ref -> Eff (refs :: ReactRefs (write :: Write | access) | eff) Unit)
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.
From react docs:
React will call the ref callback with the DOM element when the component mounts, and call it with null when it unmounts.
And this is done to avoid memory leaks (DOM references used to leak in old IEs). If we write the ref only if it's not null
, you'll prevent the original mechanism to prevent memory leaks.
So if we change this type signature (which I do like, since things are explicitly typed then) then we also need to change writeRef :: forall p s. ReactThis p s -> String -> Nullable Ref -> Eff ...
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.
Agreed. I think it makes sense to write a Nullable Ref
.
I added comments and renamed the function |
src/React/DOM/Props.purs
Outdated
-- | ``` | ||
withRef | ||
:: forall access eff | ||
. (Ref -> Eff (refs :: ReactRefs (write :: Write | access) | eff) Unit) |
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 great! Quick question: Did we want this to be Nullable Ref
? Or are we thinking that the possibility the ref is null would be handled elsewhere.
https://facebook.github.io/react/docs/refs-and-the-dom.html#caveats
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 point. Having here Nullable Ref
will be more explicit, so I prefer this. Then in the React.DOM.refToNode
we will not need to use unsafeCoerce
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.
We will only need to add a comment, that if one is attaching the ref to ReactThis
one should do that using the Nullable Ref
rather than Ref
itself.
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.
Great! Thanks!
ref = unsafeMkProps "ref" | ||
|
||
-- | You can use `writeRef` to store a reference on `Refs`. | ||
-- | ``` purescrript |
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.
Quick note: should be purescript
Looking great! @paf31 Do you have any other comments for this one? |
* Added refToNode purescript-contrib/purescript-react#100 * refToNode :: Nullable Ref -> Maybe Node purescript-contrib/purescript-react@1b0d542
String refs are not yet deprecated, but they might be at some point and React-15.6.1 docs do not event mention them. Nevertheless we should support them.
I'd be keen to change
readRef
type toi.e.
runExcept
This way it will be also use able if one sets a ref using a callback. Then on could pass
toForeigh this
and get back the ref. Setting callback refs is not included in the patch but I'd be happy to find a solution for that - maybe with a more fresh mind in the morning :)