From 317d9a710099e27396ad4ccb58a9c74d6f4d0c8a Mon Sep 17 00:00:00 2001 From: Lucid Kobold <72232219+LucidKobold@users.noreply.github.com> Date: Sat, 26 Mar 2022 20:54:35 -0500 Subject: [PATCH] Added documentation about manipulating the router within the useEffect. --- pages/calendar/[...date].tsx | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/pages/calendar/[...date].tsx b/pages/calendar/[...date].tsx index 3ed6fa6..db7501a 100644 --- a/pages/calendar/[...date].tsx +++ b/pages/calendar/[...date].tsx @@ -99,6 +99,7 @@ const DateRoute: React.FC = () => { useEffect(() => { // Checking if the slug exists and is an array. if (slug && Array.isArray(slug)) { + console.log(slug); // Grabbing the slug length const length = slug.length; @@ -107,15 +108,8 @@ const DateRoute: React.FC = () => { return parseInt(e); }); - // Checking if the slug is not "now" when the length is 1. - // ! Update this to include a check for "today". - if (length === 1 && slug[0] !== "now") { - setError(true); - return console.warn("improper date input:", slug); - } - // Checking if the slug has 2 to 3 numbers within the array. year/month/day. - if (length >= 2 && slug.length <= 3) { + if (length >= 2 && length <= 3) { // Validate that the date is valid. const newDate = validateDateInput(parsedSlug); @@ -145,10 +139,22 @@ const DateRoute: React.FC = () => { ...newDate }); } + } else if (length === 1) { + // Checking if the slug is not "now". + // ! Update this to include a check for "today". + if (slug[0] !== "now") { + setError(true); + return console.warn("improper date input:", slug); + } } } }, [slug]); + /** + * ? Pushing into the router within the use effect does not create the infinite loop. + * ? The way the validate date range or the way it is being used within a useEffect is what is creating the infinite loop. + */ + // useEffect(() => { // // Check is slug and date are valid. // if (slug && date && date !== null) {