react-pdf-highlighter icon indicating copy to clipboard operation
react-pdf-highlighter copied to clipboard

Why use scrollRef instead of ref?

Open AustenLamacraft opened this issue 5 years ago • 4 comments

I'm new to React so this is a question for my benefit. Is there a reason to use scrollRef to get at the scrollTo method of PdfHighlighter, rather than use a ref?

AustenLamacraft avatar May 24 '20 08:05 AustenLamacraft

I have been trying to convert the component example into function based and I am struggling to work out what to pass the scrollRef when doing so. Is it possible to explain?

cdpwest avatar Jun 23 '20 22:06 cdpwest

I'm a React newbie, but instead of using scrollRef I ended up passing in a ref

<PdfHighlighter
              ref={pdfHighlighter}
              ...

where pdfHighlighter is defined as

  const pdfHighlighter = useRef(null)
  const getHighlightById = id => state.highlights.find(highlight => highlight.id === id)
  const scrollToHighlightFromHash = () => {
    const highlight = getHighlightById(parseIdFromHash());
    if (highlight) {
      pdfHighlighter.current.scrollTo(highlight);
    }
  };

AustenLamacraft avatar Jun 25 '20 22:06 AustenLamacraft

@AustenLamacraft thank you! You saved my day!

theotheo avatar Oct 05 '20 15:10 theotheo

@AustenLamacraft You are a genius!

AakashVarma1995 avatar Jun 20 '22 06:06 AakashVarma1995