From 2a0ff4130fae7f5844d89e9b4a9e48cecb6b88d7 Mon Sep 17 00:00:00 2001 From: auggie2lbcf Date: Tue, 8 Apr 2025 13:49:53 -0500 Subject: [PATCH] local storage fix? --- src/components/CommentSection.tsx | 32 ++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/src/components/CommentSection.tsx b/src/components/CommentSection.tsx index cca8040..6edee1c 100644 --- a/src/components/CommentSection.tsx +++ b/src/components/CommentSection.tsx @@ -110,8 +110,10 @@ const CommentSection: React.FC = ({ postId }) => { className="w-full px-4 py-2 border border-primary-300 rounded-md focus:outline-none focus:ring-2 focus:ring-accent focus:border-transparent" required onFocus={() => { - const savedName = localStorage.getItem('name'); - if (savedName) setName(savedName); + if (typeof window !== 'undefined') { + const savedName = localStorage.getItem('name'); + if (savedName) setName(savedName); + } }} /> @@ -127,8 +129,10 @@ const CommentSection: React.FC = ({ postId }) => { className="w-full px-4 py-2 border border-primary-300 rounded-md focus:outline-none focus:ring-2 focus:ring-accent focus:border-transparent" required onFocus={() => { - const savedEmail = localStorage.getItem('email'); - if (savedEmail) setEmail(savedEmail); + if (typeof window !== 'undefined') { + const savedEmail = localStorage.getItem('email'); + if (savedEmail) setEmail(savedEmail); + } }} /> @@ -153,16 +157,18 @@ const CommentSection: React.FC = ({ postId }) => { type="checkbox" id="save-info" className="h-4 w-4 text-accent border-primary-300 rounded focus:ring-accent" - checked={localStorage.getItem('saveInfo') === 'true'} + checked={typeof window !== 'undefined' && localStorage.getItem('saveInfo') === 'true'} onChange={(e) => { - const saveInfo = e.target.checked; - localStorage.setItem('saveInfo', saveInfo.toString()); - if (saveInfo) { - localStorage.setItem('name', name); - localStorage.setItem('email', email); - } else { - localStorage.removeItem('name'); - localStorage.removeItem('email'); + if (typeof window !== 'undefined') { + const saveInfo = e.target.checked; + localStorage.setItem('saveInfo', saveInfo.toString()); + if (saveInfo) { + localStorage.setItem('name', name); + localStorage.setItem('email', email); + } else { + localStorage.removeItem('name'); + localStorage.removeItem('email'); + } } }} />