Skip to content

Commit

Permalink
Handle forwardedRef properly
Browse files Browse the repository at this point in the history
  • Loading branch information
RoyEJohnson committed May 14, 2024
1 parent 98d8d6e commit a052faa
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/app/components/MainContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,18 @@ const ContentStyles = styled(({ textSize, ...props }) => <DynamicContentStyles {

// tslint:disable-next-line:variable-name
const MainContent = React.forwardRef<HTMLDivElement, React.PropsWithChildren<Props>>(
({book, children, className, ...props}, ref) => {
({book, children, className, ...props}, forwardedRef) => {
const initialLoad = React.useRef(true);
const ref = React.useRef<HTMLDivElement>(null);

React.useImperativeHandle(forwardedRef, () => ref.current as HTMLDivElement);

React.useEffect(
() => {
if (initialLoad.current) {
initialLoad.current = false;
} else {
(ref as React.MutableRefObject<HTMLDivElement>).current?.focus();
ref.current?.focus();
}
},
[children, ref]
Expand Down

0 comments on commit a052faa

Please sign in to comment.