-
Notifications
You must be signed in to change notification settings - Fork 66
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
How to obtain a component ref in a function component? #240
Comments
This is normal, the The equivalent of you JS code is: // Dummy props to make the compiler happy
type Props =
{|
Count : int
|}
let App =
FunctionComponent.Of<Props>(fun props ->
let ref = Hooks.useRef<Element option> None
div [ RefValue ref]
[
button
[
OnClick (fun _ ->
JS.console.log ref.current
)
] [ str "Click me" ]
]
) If you still need more help, we would need the JavaScript code that you are trying to reproduce. |
I am unclear on what the equivalent JS is, but what I'm trying to replicate is being able to access type private Props =
{
Title: string
}
type SomeComponent(initialProps: Props) =
inherit Fable.React.PureStatelessComponent<Props>(initialProps)
let onPress (sourceReactComponentInstance: ReactElement) : unit =
// Open a ReactXP Popup using sourceReactComponentInstance as the anchor
override this.render() =
button
[
// It's the "this" reference here that I am unable to find an equivalent for in a function-based component
OnClick (fun _ -> onPress this)
]
[ str this.props.Title ] |
Sorry, without an example of what is expected it is difficult to help you. I tried to look at the documentation of ReactXP because I think this is what you use. But it seems like it doesn't use React but their own custom stuff. For example, they use So I feel, like you need to create a binding for their library/ecosystem. In React, the |
Whatever it is, it's what I need to get hold of from within a function component. Is that possible? This is not a ReactXP-specific question - I just happen to be using it. The point of the question is to understand how to do the equivalent of accessing the |
Looking at an example from ReactXP code, the code is very similar to the one from @MangelMaxime above. See how the reference is captured and passed through So I would use the same code. If you have a problem with the types, you can use // Declare ref
let anchor = Hooks.useRef<Element option> None
// Capture ref
div [ RefValue anchor] ...
// Access ref
unbox anchor.current.Value |
Hi,
I'm basically trying to figure out the equivalent of the following JS(X):
That is, I need to obtain a reference to the
ReactElement
representing the component itself. Note that I am making an assumption that the above JS is actually correct and thatcomponentRef
is a reference to the component itself - I haven't actually verified this.If I do the obvious thing:
It doesn't work because I'm capturing a
ReactElement
rather than the component itself.If I try to use a
dom.div
withProps.RefValue
, it captures a browser element rather than aReactElement
(let alone the component).So I'm unclear on how I obtain a reference to the component itself from within the implementation of a function component. In a class-based component, one would simply use
this
, but I can't use a class-based component.Thanks
PS. In case you're wondering, my use case is I am trying to get a reference to the component to pass it to a ReactXP
Popup
as an anchor. ThePopup
needs to be anchored to the component that displays it.The text was updated successfully, but these errors were encountered: