Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

247
Vistas
My react child callback returns the previous data (navbar)

I am learning React and I have a problem with my childCallback, for return data from child to parent.

I make a navbar and when I click on the navButton, the parent receive the name of the precedent button, I have to click two times for display the good data.

Here is my code :

//PARENT COMPONENT
export const Profil = () => {
  const [navActive, setNavActive] = useState("infos");

  const handleCallback = (childData, e) => {
    setNavActive(childData);
  };

  // View
  return (
    <div>
      <Navbar callback={handleCallback} />
      <Infos />
      <Gpg />
      {navActive}
    </div>
  );
};

//CHILD COMPONENT 
export const Navbar = ({ callback }) => {
  // Nav Items
  let nav = [
    { key: 1, name: "infos" },
    { key: 2, name: "gpg" },
    { key: 3, name: "compte" },
    { key: 4, name: "otp" },
    { key: 5, name: "journal" },
  ];

  // State
  const [activeTitle, setActiveTitle] = useState(nav[0].name);

  // Change the selected nav
  function selectNav(e) {
    let selected = e.target.innerText;
    setActiveTitle(selected);
  }

  const onTrigger = () => {
    callback(activeTitle);
  };

  // View
  return (
    <Nav tabs>
      {nav.map((item) => (
        <NavItem key={item.key} onClick={onTrigger}>
          <NavLink
            style={{ cursor: "pointer" }}
            onClick={selectNav}
            active={activeTitle === item.name}
          >
            {item.name}
          </NavLink>
        </NavItem>
      ))}
    </Nav>
  );
};

I don't know how to correct that.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Try to remove onclick from NavItem and call your callback function from selectNav function directly.

about 4 years ago · Juan Pablo Isaza Denunciar

0

The problem come from the setActiveTitle(selected) in Navbar, with console.log I test and the value is good before this.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda