-
Notifications
You must be signed in to change notification settings - Fork 33
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
How to ref
er to an element? Missing docs?
#77
Comments
Here's an example using
Sorry, I don't understand what this is saying. Binding |
In regular React, the |
Here's a minimal example of opening a foreign import showModal :: Node -> Effect Unit instead of using |
Oh, thank you! So, am I correct in my understanding that this line initializes ref <- useRef null and then the actual initialization happens here? , R.dialog
{ ref
, children: [ R.text "Dialog" ]
} So, like, the |
Although, no, even with unsafe magic I don't think you can make |
However, if that is correct, that means that the |
Another thing I don't understand is why
FWIW, I've never worked with React before starting to use this library (I wasn't even a web-programmer for that matter), so I'm coming to this library with completely clean mind 😊 |
A mkCounter :: Component {}
mkCounter = do
component "Counter" \_ -> React.do
ref <- useRef 0
let
handleClick = Events.handler_ do
current <- readRef ref
writeRef ref (current + 1)
next <- readRef ref
Console.log ("You clicked " <> show next <> " times!")
pure $
R.div_
[ R.button
{ onClick: handleClick
, children: [ R.text "Click me!" ]
}
] That would be equivalent to this JS function Counter() {
let ref = useRef(0);
function handleClick() {
ref.current = ref.current + 1;
console.log('You clicked ' + ref.current + ' times!');
}
return (
<button onClick={handleClick}>
Click me!
</button>
);
} The official React documentation goes into greater detail about |
So, suppose I used a ref <- useRef 0 and then assigned , R.dialog
{ ref
, children: [ R.text "Dialog" ]
} Does that imply the
? |
No, that example wouldn't compile. In ref <- useRef 0
, R.dialog
{ ref
, children: [ R.text "Dialog" ]
} it has type |
Reassigning ref <- useRef null
-- ...
, R.dialog
{ ref
, children: [ R.text "Dialog" ]
} The |
Okay, I see, thank you. Part of my confusion was due to the fact that So, how about adding to the library a helper function |
And yeah, I can send a PR |
This is not really anything to do with FFI, or with React, this is just how type inference works in PureScript. Maybe it helps to think of a simpler example: x = Nothing
foo :: Maybe Int -> String
foo = case _ of
Just 1 -> "one"
_ -> "something else"
bar = foo x The type of ref <- useRef null
-- ...
, R.dialog
{ ref
, children: [ R.text "Dialog" ]
} |
While at it, I think it might also be useful to add an alias |
@pete-murphy so what do you think, would you accept such change? |
Hm, I'm not really sold on the value of adding a specialized There is some precedent for having specialized hooks: I think additional documentation would certainly be welcome, if that would serve the same purpose. |
Why? You yourself mentioned that it is frequent usecase for
We can chose another naming. Like |
Refs are part of react, but accepting ref props as a way to externally expose dom elements is a feature from react-dom. As such, the helper you’re proposing would need to live there, not in this library. The type would be different for react-native, for example. Or if you’re writing a custom component which allows refs as props in some way. This is one of those things that isn’t explained much here in the purescript library because its react stuff, and already documented there. I’d suggest reading up on their docs for dom refs and how/when to use them. That could be clearer here, but that’s the way most of this library is, assuming react knowledge. The null is also important. It’s the value of the ref up until the child component you’re passing the ref to is rendered and mounted. It’s set asynchronously when the child is ready, not when the parent defining it renders. A bit weird, but it’s basically a callback, one that leaves the “when” up to you (usually a useEffect or event handler). You could also be putting the ref on a child which conditionally renders, which could reintroduce the null value. Not sure of that helps |
Okay, gotcha.
Unfortunately their docs don't contain purescript examples 😉 I did read them before asking on PureScript discorse, and then in absence of answers (which implies nobody there knew either) asking here. For PureScript ecosystem I think it's better to document the PureScript library. Especially so, since in my experience React Hooks library is way better than Halogen, but then Halogen is well documented, whereas React Hooks not so much. I am planing to write documentation for refs to close this issue (if nobody beats me to it, which I'm completely fine with), just didn't get to it yet. |
So, got some spare time, wrote documentation on how to use Seeing surprising opposition for adding helpers to the library, I decided to mention them in the docs instead, I hope it still will simplify some newbie's life in the future. |
I'd actually be pro adding the |
Was first brought up on the discourse forum.
It's often useful to refer to one DOM element from another. So for example you may have a modal window
Component
/JSX
, and you have a button that would callshowModal
on it. So you want the button to refer to the window.That's provided by both Halogen and the original React.
Now, React Hooks presumably provides something similar… This is hinted by the existence of
ref
field in eachJSX
, and byuseRef
presence. But whether and how it works is completely unknown.The
useRef
returns adata UseRef
andRef
, but the former is an opaquedata
with zero functions for it, and the latter is just a container for an arbitrary value you'd pass over to the call. So I've no idea what it could be used for.Similarly, the
ref
fields are supposed to contain someNode
, but again that requires for a functionJSX -> Node
to exist, which it doesn't.So what's the status of this feature, and what these
ref
anduseRef
are for anyway?The text was updated successfully, but these errors were encountered: