0

I got a random generated search hash router and I don't know how pass state to it. Here is what I tried so far.

<Link to={`/mahjongGame?roomCode=${randomCodeGenerator(4)}`} state={{ dataForm:data }}><button className="game-button yellow" >Confirm2</button></Link>

This is the route path.

<Route path='/mahjongGame' exact component={MahjongGame} />

This is the result of console.log(props.location) in the routed page

{pathname: '/mahjongGame', search: '?roomCode=wYTj', hash: '', state: undefined, key: 'ab5in5'}
Anson Chan
  • 103
  • 1
  • 6

1 Answers1

-1

Not sure how you are logging location with props.location. But I tried this and it works,

  const location = useLocation();
  const dataForm = location.state?.dataForm || null;
Nazrul Chowdhury
  • 1,483
  • 1
  • 5
  • 11
  • I tried before. It doesn't work. Maybe it's because hashRouter doesn't support props.state when routing to another page. I am thinking of using Browsing Router to do that. – Anson Chan Jul 16 '23 at 12:23
  • https://codesandbox.io/s/optimistic-keller-lrwflr?file=/src/App.js take a look. It is working, or could be that i misunderstood something in you question. Sorry about the messy code! – Nazrul Chowdhury Jul 16 '23 at 12:32
  • I need to use search in my routing, the expected result should be like this: `{pathname: '/mahjongGame', search: '?roomCode=610mo', hash: '', state: {…}, key: '51ka0l'}` But I found that if I use the following code, the page need to refresh first to see the page, otherwise it's blank page. `` – Anson Chan Jul 16 '23 at 12:40