ForwardedRef-Components: ref cannot be accessed on useLayoutEffect lifecycle
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 38/100
- Issue type
- Bug
- Clarity
- Mostly clear
- Activity status
- Stale
- Tech stack
- react
- Domain
- frontend
Research direction
Inspect the forwarded-ref implementations in Button and InputText, including ObjectUtils.combinedRefs and the elementRef initialization shown in the issue. Reproduce the case with a parent useLayoutEffect, then add or update a regression test; done means the forwarded ref is available when that effect runs.
Written by the indexing model from the issue text.
Description
Describe the bug
A given ref to component can not be accessed in useLayoutEffect. The problem exists in Button and InputText, I think other components may also be affected, maybe only those which do not use useImperativeHandle.
function MyButton(){
const ref = useRef(null);
useLayoutEffect(() => ref.current.focus());
return <Button ref={ref}>Click</Button>
}
Error: ref.focus is null
Here are some parts from sourcecode of Button:
export const Button = React.forwardRef((inProps, ref) => {
const elementRef = React.useRef(ref);
React.useEffect(() => {
ObjectUtils.combinedRefs(elementRef, ref);
}, [elementRef, ref]);
const rootProps = mergeProps(
{
ref: elementRef,
(...)
},
(...)
);
return (
<>
<button {...rootProps}>
(...)
</button>
</>
);
}
useEffect will synchronize both ref. Therefore useLayoutEffect which runs earlier can not access the forwardedRef.
I do not understand why ref should be initValue for elementRef.
const elementRef = React.useRef(ref);
Possible solution with custom hook:
function useForwardedRef(forwardedRef){
const ref= React.useRef();
if(forwardedRef) return forwardedRef;
return ref;
}
export const Button = React.forwardRef((inProps, ref) => {
const elementRef = useForwardedRef(ref);
return (
<>
<input ref={elementRef} {...rootProps} />
(...)
</>
}
Maybe Utils.combinedRef will be obsolete.
Reproducer
No response
System Information
react 18.2
primereact 10.5.1
Steps to reproduce the behavior
No response
Expected behavior
No response
- Dominant language
- CSS
- Stars
- 8.3k
- Forks
- 1.2k
- PR merge metrics
- No merged PRs in 30d
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
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 primefaces/primereact
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
primefaces/primereact#8552 ·
-
Difficulty 1/5 1-3 hours Newbie friendliness 88/100
primefaces/primereact#8550 ·
-
Row checkbox selection tick is invisible in Landing Page Theme sectionPossibly taken @Prakash1185 claimed this 173 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
primefaces/primereact#8545 ·
-
Dependency conflict between jspdf v4 and jspdf-autotable during npm installPossibly taken @Kamran92 claimed this 187 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
primefaces/primereact#8537 ·
-
Difficulty 3/5 1-2 days Newbie friendliness 58/100
primefaces/primereact#8551 ·
All issues in primefaces/primereact
Similar issues
-
[BUG]: Shared links drop the configured basePath on GitHub PagesPossibly taken @anshulchikhale30-p claimed this today. Openbug
Difficulty 1/5 Under an hour Newbie friendliness 88/100
StabilityNexus/IdentityTokens-EVM-Frontend#182 · 1 comment ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
-
Difficulty 2/5 Under an hour Newbie friendliness 82/100
Leonxlnx/taste-skill#216 ·
Maintainers usually reply within 1 day
-
area: ui Bug Payload OSS
Difficulty 1/5 Under an hour Newbie friendliness 88/100
payloadcms/payload#19839 ·
Maintainers usually reply within 1 day
-
BoxShadow.lerp returns the wrong blur style at t = 0 or t = 1Possibly taken @ArtemChikolaev claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 80/100
Maintainers usually reply within 1 day