Next.js Discord

Discord Forum

I want to avoid useState in nextjs. I dont like to use UseClient unless its necessary.

Unanswered
Spectacled bear posted this in #help-forum
Open in Discord
Avatar
Spectacled bearOP
I am new to nextjs and even react

I did create react website to test some knowledge
http://jupiterrules.com.s3-website-us-west-2.amazonaws.com/

Now i am trying to create a boiler plate for website

I thought i test a menu from example
https://dev.to/ryaddev/creating-a-responsive-navbar-using-nextjs-and-tailwind-css-48kk

import Link from "next/link";
import React, { useState } from "react";
import { FaBars, FaTimes } from "react-icons/fa";

const Navbar = () => {
    const [nav, setNav] = useState(false);

  const links = [
    {
      id: 1,
      link: "home",
    },
    {
      id: 2,
      link: "about",
    },
    {
      id: 3,
      link: "portfolio",
    },
    {
      id: 4,
      link: "experience",
    },
    {
      id: 5,
      link: "contact",
    },
  ];

  return (
    <div className="flex justify-between items-center w-full h-20 px-4 text-white bg-black fixed nav">
  ....
      <ul className="hidden md:flex">
        {links.map(({ id, link }) => (
          <li
            key={id}
            className="nav-links px-4 cursor-pointer capitalize font-medium text-gray-500 hover:scale-105 hover:text-white duration-200 link-underline"
          >
            <Link href={link}>{link}</Link>
          </li>
        ))}
      </ul>

      <div
        onClick={() => setNav(!nav)}
        className="cursor-pointer pr-4 z-10 text-gray-500 md:hidden"
      >
        {nav ? <FaTimes size={30} /> : <FaBars size={30} />}
      </div>
...
     );
};

export default Navbar;


This navbar component uses useState.

I want to get rid of it because i want everything be static html. No use client

22 Replies

Avatar
Asian black bear
What you want is an interactive element that changes its state based on the interaction of the user on the client-side. It literally cannot be fully static HTML (ignoring some advanced CSS things) and thus that part of the component must be a client component.
Also there is nothing wrong with using client components.
Avatar
Spectacled bearOP
Oh i need to find a way to get rid of useState as much as possible
Navigations are static. Does it need useState?
I took this handle it via tailwind css
Avatar
Asian black bear
Oh i need to find a way to get rid of useState as much as possible
No, you don't need to.
Having state or client component is not wrong and not bad.
No clue why you came to that conclusion.
Avatar
Spectacled bearOP
bad client component is bad
I want everything static html as much
Only reason nextjs is tempting
so i am here
Seo friendly
Avatar
Asian black bear
Client components are pre-rendered and don't negatively affect SEO
Avatar
Spectacled bearOP
I am completely new to nextjs
just thought i make a boiler plate. i did run lamadex nextjs example to get hands on with nextjs
Avatar
Asian black bear
Which is why you shouldn't argue without having the full picture
Avatar
Ojos Azules
bud, you can't avoid interactivity in your website and interactivity is possible via useState and it doesn't affect your performance because as the mod said above that client components get pre-rendered but what u can actually do is make your client components as small as possible instead od the whole navbar like creating the div element in your navbar code which has the onClick event listener
i created a nextjs-commerce store and it has lots of client components in it but it still got 100% performance and 91% SEO the reason why it doesn't have 100% SEO is that the client didn't add any alt text on images from the shopify backend
Avatar
Havana
Don't get too hung up on making everything a server component like I used to. Sometimes client components come in handy. Also, think about breaking down more components to get what you're aiming for.
Avatar
Spectacled bearOP
give me a good recording window software. I use to use chrome plugins. Now all of them unusable. Its like good working plugin no longer work
Yeh i removed use state from menu state