ForwardedRef-Components: ref cannot be accessed on useLayoutEffect lifecycle

Aperta
#8,391 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
38/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Ferma
Stack tecnologico
react
Ambito
frontend

Direzione di ricerca

Esamina le implementazioni delle forwarded ref in Button e InputText, inclusi ObjectUtils.combinedRefs e l’inizializzazione di elementRef mostrata nell’issue. Riproduci il caso con un parent useLayoutEffect, quindi aggiungi o aggiorna un test di regressione; il lavoro è completato quando la forwarded ref è disponibile nel momento in cui viene eseguito quell’effetto.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Type: Enhancement
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

Lingua principale
CSS
Stelle
8.3k
Fork
1.2k
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di primefaces/primereact

Tutte le issue di primefaces/primereact

Issue simili

Altre issue su Web Dev

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.