How to pass params with history.push/Link/Redirect in react-router v4?

前端 未结 10 1098
你的背包
你的背包 2020-11-22 04:27

How can we pass parameter with this.props.history.push(\'/page\') in React-Router v4?

.then(response => {
       var r = this;
        if (re         


        
10条回答
  •  北荒
    北荒 (楼主)
    2020-11-22 05:14

    I created a custom useQuery hook

    import { useLocation } from "react-router-dom";
    
    const useQuery = (): URLSearchParams => {
      return new URLSearchParams(useLocation().search)
    }
    
    export default useQuery
    

    Use it as

    const query = useQuery();
    const id = query.get("id") as string
    

    Send it as so

    history.push({  
     pathname: "/template",
     search: `id=${values.id}`,
    });
                      
    

提交回复
热议问题