将状态添加到<Link>会覆盖其他属性,例如“搜索”和“参数”

时间:2019-11-11 13:27:19

标签: reactjs react-router-dom

我正在将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是相同的,但是locationmatch是完全不同的。

我想要什么?
我希望location.searchmatch.params保持状态一致或不保持状态。我该如何处理?

版本:react-router-dom: 5.1.2,

1 个答案:

答案 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>