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

384
Vistas
How do I pass useState from one component to another?

I am trying to make a login modal that changes to the signup modal when the signup button is clicked within the Login.js. Currently, I have a header with a login button. When the login button is clicked, the current state is displayed (default value is ). I would like the signup button within Login.js to be able to update the state in order for that modal to .

Header.js:

import React, { useState } from 'react';
import Login from "../modals/Login";
import Signup from "../modals/Signup";

export default function Header() {

    const [lmodal, setModal] = useState(<Login />);

    return (
...
    {lmodal}
...
)
}

Login.js

import React, { useState } from 'react';
import Signup from "../modals/Signup";

export default function Login() {

return (
...
**// Clicking the button should change the state from Header.js to <Signup />**
 <button onClick={() => { setModal(<Signup />) }} className="btn btn-primary">Signup</button>
...
)
}

Thanks for the help in advance!

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

0

you can't setModal from within the Login component because setModal is in the Header component.

about 4 years ago · Juan Pablo Isaza Denunciar

0

States and components are different in concept. What you coould possible do to reach the wanted behavior is to use the state to toogle the component, for exemple:

import React, { useState } from 'react';
import Login from "../modals/Login";
import Signup from "../modals/Signup";

export default function Header() {

    const [modal, setModal] = useState(false);

    return (
...
    {modal && <Login />}
    <Signup modal={modal} setModal={setModal}/>
...
)}
import React, { useState } from 'react';
import Signup from "../modals/Signup";

export default function Login({modal, setModal}) {

return (
...
 <button onClick={() => setModal(true)} className="btn btn-primary">Signup</button>
...
)
}

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