Skip to content

Commit

Permalink
enhancement: scroll admissions content into view using effect hook
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesyaputra committed Jan 22, 2021
1 parent 88441e4 commit 27cf1f0
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
21 changes: 14 additions & 7 deletions src/pageContent/Admissions/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useRef } from "react";
import React, { useEffect, useRef } from "react";
import Link from "next/link";
import { useRouter } from "next/router";
import Button from "src/components/Button";
Expand All @@ -22,6 +22,15 @@ const AdmissionsContent: React.FC<OwnProps> = ({
const currPageNum = parseInt(slug[slug.length - 1]);

const contentRef = useRef(null);
const firstUpdate = useRef(true);
useEffect(() => {
if (firstUpdate.current) {
firstUpdate.current = false;
return;
}

contentRef.current.scrollIntoView();
});

const renderNavItems = (navItems, chapter) => {
return navItems[chapter].map((navItem, index) => {
Expand Down Expand Up @@ -85,13 +94,12 @@ const AdmissionsContent: React.FC<OwnProps> = ({
<div className={`flex flex-row justify-between mt-20`}>
{currPageNum > 1 && (
<Button
onClick={async () => {
await router.push(
onClick={() => {
router.push(
`/admissions/${slug[0]}/${currPageNum < 10 ? "0" : ""}${
currPageNum - 1
}`
);
await contentRef.current.scrollIntoView();
}}
style={`mr-auto`}
>
Expand All @@ -100,13 +108,12 @@ const AdmissionsContent: React.FC<OwnProps> = ({
)}
{currPageNum < Object.keys(navItems[chapter]).length && (
<Button
onClick={async () => {
await router.push(
onClick={() => {
router.push(
`/admissions/${slug[0]}/${currPageNum < 10 ? "0" : ""}${
currPageNum + 1
}`
);
await contentRef.current.scrollIntoView();
}}
style={`ml-auto`}
>
Expand Down
2 changes: 1 addition & 1 deletion src/pageContent/Home/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ const HomeContent: React.FC = () => {
className={`lg:hidden col-start-6 col-span-7 row-start-1 row-span-6`}
>
<div
className={`mt-6 ml-6 grid grid-cols-7 grid-rows-6 h-full w-full`}
className={`pt-6 pl-6 grid grid-cols-7 grid-rows-6 h-full w-full`}
>
<div className={`col-span-6 row-span-5 bg-pinus-blue`} />
</div>
Expand Down

1 comment on commit 27cf1f0

@vercel
Copy link

@vercel vercel bot commented on 27cf1f0 Jan 22, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.