Customize Consent Preferences

We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.

The cookies that are categorized as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... 

Always Active

Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.

No cookies to display.

Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.

No cookies to display.

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.

No cookies to display.

Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.

No cookies to display.

Advertisement cookies are used to provide visitors with customized advertisements based on the pages you visited previously and to analyze the effectiveness of the ad campaigns.

No cookies to display.

Pass a parameter/attribute from one React screen to another

Tiempo de lectura: 2 minutos

Hello, today we are going to learn how we can pass a parameter from one React screen to another.

Step 1: Installing React Router v6

If you haven’t already, install React Router v6:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
npm install react-router-dom@next
npm install react-router-dom@next
npm install react-router-dom@next

Step 2: Configuring React Router

In your App.js file, set up routes using BrowserRouter and Route:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
// App.js
import React from 'react';
import { BrowserRouter as Router, Route, Routes } from 'react-router-dom';
import Screen1 from './Screen1';
import Screen2 from './Screen2';
function App() {
return (
<Router>
<Routes>
<Route path="/" element={<Screen1 />} />
<Route path="/dashboard/:userId" element={<Screen2 />} />
</Routes>
</Router>
);
}
export default App;
// App.js import React from 'react'; import { BrowserRouter as Router, Route, Routes } from 'react-router-dom'; import Screen1 from './Screen1'; import Screen2 from './Screen2'; function App() { return ( <Router> <Routes> <Route path="/" element={<Screen1 />} /> <Route path="/dashboard/:userId" element={<Screen2 />} /> </Routes> </Router> ); } export default App;
// App.js
import React from 'react';
import { BrowserRouter as Router, Route, Routes } from 'react-router-dom';
import Screen1 from './Screen1';
import Screen2 from './Screen2';

function App() {
  return (
    <Router>
      <Routes>
        <Route path="/" element={<Screen1 />} />
        <Route path="/dashboard/:userId" element={<Screen2 />} />
      </Routes>
    </Router>
  );
}

export default App;

Step 3: Navigating with ID

In your Screen 1 component (Screen1.js), use useNavigate to handle navigation:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
// Screen1.js
import React from 'react';
import { useNavigate } from 'react-router-dom';
const Screen1 = () => {
const navigate = useNavigate();
const handleClick = (id) => {
navigate(`/dashboard/${id}`);
};
return (
<div>
{/* Screen 1 content */}
<button onClick={() => handleClick(123)}>Go to Dashboard with ID 123</button>
</div>
);
};
export default Screen1;
// Screen1.js import React from 'react'; import { useNavigate } from 'react-router-dom'; const Screen1 = () => { const navigate = useNavigate(); const handleClick = (id) => { navigate(`/dashboard/${id}`); }; return ( <div> {/* Screen 1 content */} <button onClick={() => handleClick(123)}>Go to Dashboard with ID 123</button> </div> ); }; export default Screen1;
// Screen1.js
import React from 'react';
import { useNavigate } from 'react-router-dom';

const Screen1 = () => {
  const navigate = useNavigate();

  const handleClick = (id) => {
    navigate(`/dashboard/${id}`);
  };

  return (
    <div>
      {/* Screen 1 content */}
      <button onClick={() => handleClick(123)}>Go to Dashboard with ID 123</button>
    </div>
  );
};

export default Screen1;

Step 4: Retrieving ID in Screen 2

In your Screen 2 component (Screen2.js), use useParams to retrieve the ID:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
// Screen2.js
import React from 'react';
import { useParams } from 'react-router-dom';
const Screen2 = () => {
const { userId} = useParams();
return (
<div>
{/* Screen 2 content */}
<p>User ID: {userId}</p>
</div>
);
};
export default Screen2;
// Screen2.js import React from 'react'; import { useParams } from 'react-router-dom'; const Screen2 = () => { const { userId} = useParams(); return ( <div> {/* Screen 2 content */} <p>User ID: {userId}</p> </div> ); }; export default Screen2;
// Screen2.js
import React from 'react';
import { useParams } from 'react-router-dom';

const Screen2 = () => {
  const { userId} = useParams();

  return (
    <div>
      {/* Screen 2 content */}
      <p>User ID: {userId}</p>
    </div>
  );
};

export default Screen2;

Step 5: Running the Project

Make sure your application is running:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
npm start
npm start
npm start

Open your browser and visit http://localhost:3000. Clicking the button on Screen 1 should correctly redirect you to Screen 2 with the user ID displayed.

0

Leave a Comment