Search code examples
reactjshyperlinkreact-routerreact-router-domreact-props

can't pass props using Link in react router dom


enter image description here


 <Link to={{pathname:"/Watch", movie:movie}} >

using this to pass props over Watch page but can't get any value.

I am new to react so there might be silly mistake or might be insufficient information. please let me know regarding any extra info


Solution

  • If you are using react-router-dom v5 then route state is a property on the to object of the Link.

    RRDv5 Link

    <Link to={{ pathname: "/Watch", state: { movie } }}>...</Link>
    

    If using react-router-dom v6 then state is a top-level prop.

    RRDv6 Link

    <Link to="/Watch" state={{ movie }}>...</Link>