简体   繁体   中英

Passing props with Navlink in react-router-dom (v6)

In version 5, I used to do this:

<NavLink
  key={"myRoute"}
  to={{
    pathname: "myRoute",
    state: {myState: "myStateValue"},
  }}
  className="nav-card"
>

then receive props with useLocation location.state

But what happened when I upgraded to version 6 is that location.state is null

Try doing this:

<NavLink
  key={"myRoute"}
  to="myRoute"
  state={{ myState: "myStateValue" }}
  className="nav-card"
>

then to receive the props:

const location = useLocation()
const { myState } = location.state

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM