avatarRajdeep Singh

Summary

The provided web content explains how to pass parameters using the react-router-dom Link component in React applications, detailing the process of declaring and accessing these parameters.

Abstract

The article titled "How to pass parameters in react-router-dom Link?" by Rajdeep Singh is a technical guide aimed at React developers looking to pass data between components using react-router-dom. Singh outlines a two-step process: first, declaring the parameters within the Link component by passing an object with properties such as pathname, search, hash, and state; and second, accessing these parameters in the target component using the useLocation hook from react-router-dom. The article includes code examples, GIF demonstrations, and links to external resources such as GitHub repositories and Codesandbox demos to illustrate the concepts. Singh also touches on the differences between passing parameters in react-router version 5 and the latest version 6, providing a link to another blog post specifically addressing v6. The article concludes with an invitation to try out the author's demos and a promotional plug for an AI service called ZAI.chat, positioned as a cost-effective alternative to ChatGPT Plus.

Opinions

  • The author, Rajdeep Singh, emphasizes the importance of understanding how to pass parameters in React applications for efficient data transfer between components.
  • Singh suggests that using the react-router-dom Link component to pass parameters is a straightforward process, demonstrating this with clear code examples and visual aids.
  • The article promotes the use of the useLocation hook as a simple method to access the state passed through the Link component, enhancing the reader's understanding of React routing.
  • Singh provides additional value by linking to a follow-up blog post that addresses parameter passing in the newer react-router v6, indicating a commitment to keeping the content up-to-date with the latest React developments.
  • The inclusion of live demos and source code repositories reflects the author's dedication to practical, hands-on learning experiences for the readers.
  • At the end of the article, Singh endorses an AI service, ZAI.chat, as a recommendation to the readers, suggesting it as a more affordable option compared to ChatGPT Plus, which may appeal to cost-conscious developers.

Reactjs

How to pass parameters in react-router-dom Link?

Pass data from one component to another component with react-router (v5) Link Component.

How to pass parameters in react-router-dom Link

Hey, my name is Rajdeep Singh. In this react post, I’m telling you How to pass a parameter, state, props, and value in the react-router-dom Link Component.

{/* Link tag With out parameter in reactjs */}
<Link to="/about">About</Link>

More Read About Link Component: Click Here Github for download code: Click Here Demo and Download Code on Codesandbox.io

Demo:

Latest blog post on react-router v6

Let’s Start

pass parameters in react-router-dom Link Component with two basic steps.

Step:1. Declare Step:2. Access

Step:1.

Declare mean how to write code and pass parameter, state, props, and value.

How is it possible?

Simple when Creating a link component to react, inside to=”/about” we pass an object. In this object, we pass some properties.

  1. pathname: A string representing the path to link.
  2. search: A string representation of query parameters.
  3. hash: A hash to put in the URL, e.g. #a-hash.
  4. state/any name: Inside State to value.

In my case, I pass state, But you pass any value.

Example:

{/* Link component Without parameter */}
 {/* <Link to="/about">About</Link> */}

<Link   to={
  {/* this object inside the  tag */}
       {     
         pathname: '/read',
         state:this.state
        }
  }> 
<Card.Link  >Another Link</Card.Link>
</Link>

Step:2.

Access means how to access Link Component State we Declare previous.

But How to Access it?

It is straightforward. We use a react-router-dom hook to access the state. In the first step, we import the useLocation Hook. The inside hook shows all properties. After accessing all properties with the object. check out code

Check out with console.log()

useLocation Hook with console

code

import React from "react";
import Card from "react-bootstrap/Card";
import CardGroup from "react-bootstrap/CardGroup";
import {Link, useLocation} from "react-router-dom";
function Read() {
let location = useLocation();
console.log(location);
return (
<>
<Link to="/">
<Card.Link>
   <h1 className="mt-3">Back... </h1>
</Card.Link>
</Link>
<CardGroup>
<Card>
<Card.Img
variant="top"
style={{width: "100%", height: "400px", margin: "auto"}}
src="https://source.unsplash.com/random"
/>
<Card.Body>
<Card.Title className="mt-5">Card by read</Card.Title>
<Card.Text>
{location.pathname} <br />
{location.state.earn} <br />
{location.state.love} <br />
{location.state.name} <br />
This is a wider card with supporting text below as a natural
lead-in to additional content. This content is a little bit
longer.
</Card.Text>
</Card.Body>
</Card>
</CardGroup>
</>
);
}
export default Read;

Read More:

Demo here

demo with code sandbox live app

First demo: Click Here Second demo: Click Here

Conclusion:

Sometimes our requirement passes value and props inside the link Component. use react-router-dom useLocation hook access all properties

Check My Second Demo With a real-life senior. In this project, I pass the id inside the link Component after accessing that Id. Uses Axios to get data based on Id.

Thanks for Reading 😆

React
Reactjs
Reactjs Development
React Router Dom
React Hook
Recommended from ReadMedium