0

I am passing a state to the Link component and trying to access the state using props.location.satate. Unfortunately, the state is undefined. I have tried to send the state from the to attribute using the pathname and state, still undefined.

<Route exact path="/private/orders" name="Orders" component={PrivateOrders} />

Private orders

import React from 'react'

const PrivateOrders = (props) => {
    console.log(props)
    return (
        <div>
            <h1>Orders</h1>
        </div>
    )
}

export default PrivateOrders

Link

<Link  
   to="private/orders"
   state={{
      x: "y",
      y: "y",
      z: "y",
      i: "y",
   >
      <CButton color="primary">Order Now</CButton>
</Link>

result

location: {pathname: '/private/orders', search: '', hash: '', state: undefined, key: 'lt9bpr'}
kakakakakakakk
  • 467
  • 10
  • 31

1 Answers1

1

Pass routeProps in Route like this.

<Route exact path="/private/orders" name="Orders" component={(routeProps) => <PrivateOrders {...routeProps} />} />

  • same undefined state, it should be {...routeProps} – kakakakakakakk Nov 22 '21 at 11:55
  • The `component` prop of a `Route`is supposed to take a component reference, not an anonymous React function. Use the `render` prop for this. See https://v5.reactrouter.com/web/api/Route/component – Drew Reese Nov 22 '21 at 16:37