How to preserve query parameters in react-router v4

前端 未结 3 1386
深忆病人
深忆病人 2021-01-17 13:56

Users redirected to my app after login (server on java), and they have url, which looks like this http://10.8.0.29:8083/html/?locale=RU&token=1c5c71f2-dcda-4a51-8cf6-f8

3条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-17 14:08

    In React-Router 4.3 (not sure about earlier versions), if you have a just above, something like this should work: (this is in Typescript)

    Route({ path: ..., render: (props) => function() {
      Redirect({ ...,
          to: { pathname: ... search: props.location.search, ... }})
    });
    

    Explanation: You use the render: (props) => .... proprety of the tag, instead of component: ..., because render gives you props, so then inside the you can use props.location.search and in that way access the current query params, and reuse in the redirect.

    If there's no above, maybe you cannot. I just asked here: How preserve query string and hash fragment, in React-Router 4 ?

提交回复
热议问题