我正在将Link
的有状态和无状态用作:
<Link to={{pathname: "/test/profile?id=123", state: {key: "value"}}}>Resend</Link>
<Link to="/test/profile?id=123"}>Resend</Link>
将状态添加到Link
会覆盖组件中的其他道具。
没有状态的道具如下:
{
"history": {
"length": 10,
"action": "PUSH",
"location": {
"pathname": "/test2/profile?id=123",
"state": {
"key": "value"
},
"search": "",
"hash": "",
"key": "3u9qkj"
}
},
"location": {
"pathname": "/test2/profile",
"search": "?id=123",
"hash": "",
"key": "m5btna"
},
"match": {
"path": "/test2/:type",
"url": "/test2/profile",
"isExact": true,
"params": {
"type": "profile"
}
}
}
并带有状态:
{
"history": {
"length": 10,
"action": "PUSH",
"location": {
"pathname": "/test2/profile?id=123",
"state": {
"key": "value"
},
"search": "",
"hash": "",
"key": "3u9qkj"
}
},
"location": {
"pathname": "/test2/profile?id=123",
"state": {
"key": "value"
},
"search": "",
"hash": "",
"key": "3u9qkj"
},
"match": {
"path": "/test2/:type",
"url": "/test2/profile?id=123",
"isExact": true,
"params": {
"type": "profile?id=123"
}
}
}
因此,history
是相同的,但是location
和match
是完全不同的。
我想要什么?
我希望location.search
和match.params
保持状态一致或不保持状态。我该如何处理?
版本:react-router-dom: 5.1.2,
答案 0 :(得分:0)
您可以尝试这种方式
<Link
to={{
pathname: "/test/profile",
search: "?id=123",
state: { key: "value" }
}}
>
Resend State
</Link>
<Link to="/test/profile?id=123">Resend no state</Link>