-1

As already mentioned above, I need to pass a props in a Link and read it in the passed component.

In particular I have a Card component where props are passed; of these props I need to pass "props.geo" in the City component.

I tried to do it as shown in the code but noticed that nothing is printed.

Card.js

import {Link } from "react-router-dom";

export default function Card(props) {

    return (
        <div className="card mt-3 me-3" style={{ width: "18rem" }}>
            <div className="card-body">
                <h5 className="card-title">{props.name}</h5>
                <h6 className="card-subtitle mb-2 text-muted">{props.username}</h6>
                <p className="card-text">{props.email} <Link to={`mailto:${props.email}`}><button>Contact me</button></Link> </p>
                <a className="card-link"><Link to="/city" state={{ geo: props.geo }}  target="_blank">View city</Link></a>
            </div>
        </div>
    )
}

City.js

import { useLocation } from 'react-router-dom'

export default function City(props) {
    const location = useLocation()
    console.log(props, " props");
    console.log(location, " useLocation Hook");
    return (
        <h1>You are in City Component</h1>
    )
}

Where is the problem?

Sarah
  • 292
  • 1
  • 10

0 Answers0