Skip to content

Accessing refs in override components #5016

Answered by Rall3n
Merynek asked this question in Q&A
Discussion options

You must be logged in to vote

Hello @Merynek,

replacing the innerRef prop particularly on the Menu component could break functionality.
If you want/need to do this, you should still add the ref to the original innerRef prop.
To do this you create a callback function which you set as the innerRef prop, in which you set both refs.

const Menu = ({ innerRef, ...props }: MenuProps) => {
  const theRef = useRef<HTMLDivElement>(null);
  
  const setRef = useCallback((ref) => {
    theRef.current = ref;
    innerRef(ref);
  }, [innerRef]);  

  return <components.Menu {...props} innerRef={setRef} />
}

Replies: 1 comment 8 replies

Comment options

You must be logged in to vote
8 replies
@Rall3n
Comment options

@Merynek
Comment options

@Nantris
Comment options

@JanderSilv
Comment options

@Nantris
Comment options

Answer selected by Merynek
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
4 participants