Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

381
Views
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 answers
Answer question

0

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

about 4 years ago · Juan Pablo Isaza Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!