Pass object through Link in react router

后端 未结 6 1179
轮回少年
轮回少年 2020-12-01 07:24

Is it possible to pass an object via Link component in react-router?

Something like:
Click <

6条回答
  •  暖寄归人
    2020-12-01 08:20

    Using react-roter-dom ver 5.1.2 and es6

    I solved the problem like this first I store it in the search part of Link to object meaning it is part of the url and supports a bookmark etc.

    < Link to={{
              pathname: `${match.url}/${dogName}`,
              search: `choosenDog=${JSON.stringify({ ...dog })}` //dog is the object to pass along
            }
            } >
    

    And then on the link we goto I get hold of it like this

    const query = useQuery();
    const dogString = query.get('choosenDog');
    const dogObject  = JSON.parse(dogString);
    
    

提交回复
热议问题