Skip to content

Commit

Permalink
Handle forwardedRef properly
Browse files Browse the repository at this point in the history
  • Loading branch information
RoyEJohnson committed Jun 28, 2024
1 parent d8f64dc commit da7eaad
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 da7eaad

Please sign in to comment.