首页 > 技术文章 > 使用 react-router-dom v5 查询query 参数的方法

qq735675958 2020-12-30 20:41 原文

文档 是这样的 

function useQuery() {
  return new URLSearchParams(useLocation().search);
}

------使用

function demo (){
  const history = useHistory();
  history.get('name')
}

-------------------自己写--------------------------

import { useLocation} from 'react-router-dom';
const qs = require('query-string');

export function useQuery() {
  const { search } = useLocation();
  return qs.parse(search);
}
 
------使用
const query = useQuery();
console.log(query)  // {id: "1222", name: "zhansan"}
 


推荐阅读