0

I have a function to display user dashboard navigation. This function should set active funciotn to this elem waht is click default is 1 starter element/link.But i get infinite loop.

const UserLinks = [
  {
    name: "User Details",
    linkTo: "/dashboard",
  },
  {
    name: "Change Password",
    linkTo: "/dashboard/changePassword",
  },
];
const DasboardLinks = () => {

  const [active, setActive] = useState(1);

  return (
    <SectionNavigation>
      <ul>
        { UserLinks.map((link, i) => (
              <li key={i} className={active === i ? "active" : ""}
            onClick={setActive(i)}>
                <Link to={link.linkTo}>{link.name}</Link>
              </li>
            ))}
      </ul>
    </SectionNavigation>
  );
};

When it is executed i get Too many re-renders. React limits the number of renders to prevent an infinite loop.How can i fix it to work.

1 Answer 1

2

You are calling setActive instead of passing a function:

onClick={() => setActive(i)}>
Sign up to request clarification or add additional context in comments.

1 Comment

what a stupid mistake thanks i didnt swa it :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.