Next.js Discord

Discord Forum

Cant't create sidebar useState because of async function

Unanswered
False honey ant posted this in #help-forum
Open in Discord
False honey antOP
I'm new to Nextjs but I've managed to build my first complete app. I am trying to create a sidebar that opens and closes on smaller devices. So, the button to open and close will be seen in the navbar and the side bar, I need to pass the props from a parent page.

I am having an issue because the main page where my sidebar and navbar component is has an async function that I can't make into client side rendering. I tried converting the function but then get a black screen in the app.

this is a snippet of the page code:

const ChatPage = async ({params: {chatId}}: Props) => {
const{userId} = await auth();

if (!userId) {
return redirect('/sign-in');
}
const _chats = await db.select().from(chats).where(eq(chats.userId, userId))
console.log(_chats)
if (!_chats){
return redirect('/');
}
if (!_chats.find((chat) => chat.id === parseInt(chatId))){
return redirect('/');
}
const currentChat = _chats.find((chat) => chat.id === parseInt(chatId));
const isPro = await checkSubcription();

return (
<div className="flex h-screen">
<div className="flex w-full h-screen">
{/* chat sidebar /}
<div className="flex-[1] max-w-xs">
<ChatSideBar chats={_chats} chatId={parseInt(chatId)} isPro={isPro} />
</div>
{/
pdf viewer /}
<div className='flex flex-col w-full h-screen divide-y'>
<div className='flex w-full '>
<DashNavHeader />
</div>
<div className='flex flex-row h-screen w-full'>
<div className="hidden md:block flex-[2]">
<PDFViewer pdf_url={currentChat?.pdfUrl || ''} />
</div>
{/
chat component */}
<div className='flex-[2] border-l-4 border-l-slate-200'>
<ChatComponent chatId={parseInt(chatId)} />

</div>
</div>
</div>
</div>
</div>
)
};
export default ChatPage

I want to add simple useState:

const [open, setOpen] = React.useState(false);

<Button className={${open ? "block md:hidden": "hidden" }} onClick={() => setOpen(!open)}>

1 Reply

@False honey ant I'm new to Nextjs but I've managed to build my first complete app. I am trying to create a sidebar that opens and closes on smaller devices. So, the button to open and close will be seen in the navbar and the side bar, I need to pass the props from a parent page. I am having an issue because the main page where my sidebar and navbar component is has an async function that I can't make into client side rendering. I tried converting the function but then get a black screen in the app. this is a snippet of the page code: const ChatPage = async ({params: {chatId}}: Props) => { const{userId} = await auth(); if (!userId) { return redirect('/sign-in'); } const _chats = await db.select().from(chats).where(eq(chats.userId, userId)) console.log(_chats) if (!_chats){ return redirect('/'); } if (!_chats.find((chat) => chat.id === parseInt(chatId))){ return redirect('/'); } const currentChat = _chats.find((chat) => chat.id === parseInt(chatId)); const isPro = await checkSubcription(); return ( <div className="flex h-screen"> <div className="flex w-full h-screen"> {/* chat sidebar */} <div className="flex-[1] max-w-xs"> <ChatSideBar chats={_chats} chatId={parseInt(chatId)} isPro={isPro} /> </div> {/* pdf viewer */} <div className='flex flex-col w-full h-screen divide-y'> <div className='flex w-full '> <DashNavHeader /> </div> <div className='flex flex-row h-screen w-full'> <div className="hidden md:block flex-[2]"> <PDFViewer pdf_url={currentChat?.pdfUrl || ''} /> </div> {/* chat component */} <div className='flex-[2] border-l-4 border-l-slate-200'> <ChatComponent chatId={parseInt(chatId)} /> </div> </div> </div> </div> </div> ) }; export default ChatPage I want to add simple useState: const [open, setOpen] = React.useState(false); <Button className={\${open ? "block md:hidden": "hidden" }`} onClick={() => setOpen(!open)}>`
make it in client component
export default function Sidebar() {
  const [open, setOpen] = useState(false);

  return (
    <>
      <button onClick={() => setOpen((prev) => !prev)}>open</button>
      {open && (
        <span
          className="fixed inset-0 bg-black/50"
          onClick={() => setOpen(false)}
        ></span>
      )}
      <div
        className={`fixed left-0 top-0 bottom-0 w-[220px] bg-white transition-transform ${
          open ? "translate-x-0" : "-translate-x-full"
        } `}
      ></div>
    </>
  );
}