How to obtain a component ref in a function component?
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 25/100
- Issue type
- Documentation
- Clarity
- Needs clarification
- Activity status
- Stale
- Tech stack
- fsharp, react
- Domain
- frontend
Research direction
The issue names no repository files or tests. Start by checking the useRef, FunctionComponent, and Props.RefValue approaches shown in the report, then verify what ReactXP Popup accepts as an anchor; done means documenting a supported approach or clearly stating whether a function component reference is available.
Written by the indexing model from the issue text.
Description
Hi,
I'm basically trying to figure out the equivalent of the following JS(X):
import { useRef } from 'react';
function MyComponent() {
const componentRef = useRef(null);
// Function to handle a button click
function handleClick() {
// Access the component using the ref object
console.log(componentRef.current);
}
return (
<div ref={componentRef}>
<button onClick={handleClick}>Click me</button>
</div>
);
}
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 that componentRef is a reference to the component itself - I haven't actually verified this.
If I do the obvious thing:
FunctionComponent.Of<Props>(
(fun props ->
let rootHook = Hooks.useRef<Option<ReactElement>> = None
let root = // create the root element, passing rootHook to things that need access to the component itself
rootHook.current <- Some root
root
)
It doesn't work because I'm capturing a ReactElement rather than the component itself.
If I try to use a dom.div with Props.RefValue, it captures a browser element rather than a ReactElement (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. The Popup needs to be anchored to the component that displays it.
- Dominant language
- F#
- Stars
- 277
- Forks
- 65
- PR merge metrics
- No merged PRs in 30d
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from fable-compiler/fable-react
-
Difficulty 3/5 1-2 days Newbie friendliness 35/100
fable-compiler/fable-react#243 · 3 comments ·
-
Difficulty 4/5 3-5 days Newbie friendliness 35/100
fable-compiler/fable-react#241 · 1 comment ·
-
Difficulty 3/5 1-2 days Newbie friendliness 35/100
fable-compiler/fable-react#239 · 3 comments ·
-
Difficulty 3/5 1-2 days Newbie friendliness 35/100
fable-compiler/fable-react#236 ·
-
Difficulty 4/5 3-5 days Newbie friendliness 18/100
fable-compiler/fable-react#232 · 7 comments · 3 reactions ·
All issues in fable-compiler/fable-react
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
radiantearth/stac-browser#1023 ·
-
needs-triage
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
-
bug quarto-hub
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
quarto-dev/q2#721 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
-
frontend maui-pilot pilot-ask question
Difficulty 2/5 1-3 hours Newbie friendliness 75/100