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

128
Views
React value passing from one component to another

I get a username from "login.jsx" and i want to pass it to the "App.js" to access in everywhere. How can I do that?

function App() {
  const [user, setUser] = useState(null);
     
     useEffect(() => {
        
    }, []);
  return (
    <BrowserRouter>
    <Offer/>
    <Navigationbar/>
      <Route path="login" element={user ? <Navigate to="/courses" /> : <Login />} />
    </Routes>
    <Footer />
    </BrowserRouter>

  );
}
about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

you can pass callback function to Login component as props and call this prop inside Login component and pass user name as argument, then inside callback function you call setUser function to update the value of user

function App() {
  const [user, setUser] = useState(null);

  const updateUser = (value) => {
      setUser(value);
  }
     
     useEffect(() => {
        
    }, []);
  return (
    <BrowserRouter>
    <Offer/>
    <Navigationbar/>
      <Route path="login" element={user ? <Navigate to="/courses" /> : <Login updateUser={updateUser} />} />
    </Routes>
    <Footer />
    </BrowserRouter>

  );
}
about 4 years ago · Santiago Trujillo Report

0

Here is How you can do that

App.jsx

function App() {
  const [user, setUser] = useState(null);
     
     useEffect(() => {
        
    }, []);

  const updateUser = (newUser) => {
     setUser(newUser);
  }

  return (
    <BrowserRouter>
    <Offer/>
    <Navigationbar/>
      <Route path="login" element={user ? <Navigate to="/courses" /> : <Login onNewUser={updateUser} />} />
    </Routes>
    <Footer />
    </BrowserRouter>

  );
}

In Login.jsx when you get new user call props.updateUser(user)

about 4 years ago · Santiago Trujillo Report

0

You can do that with React Context;

Basically, contexts are global states. You can read more here : https://reactjs.org/docs/context.html

It allows you to wrap your <App /> into a context provider and pass to your wrapper a value that you will be able to user in your App.

about 4 years ago · Santiago Trujillo 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!