【问题标题】:Reactjs location.search not working in react router v6Reactjs location.search 在 React 路由器 v6 中不起作用
【发布时间】:2022-11-28 08:17:21
【问题描述】:
**const redirect = location.search? location.search.split('=') : "/account" ;
 useEffect(() => {
    if (error) {
      alert.error(error);
      dispatch(clearErrors());
    }
    if (isAuthenticated) {
      navigate(redirect);
    }
  }, [dispatch, error, alert, history, isAuthenticated, redirect]);**

这不起作用任何人请告诉我我在这个地方更换了什么,以便我的这个功能工作。我非常感谢你。

【问题讨论】:

标签: reactjs react-router


【解决方案1】:

location.search 不会给你 URL 的路径,只有查询参数(如果 URL 中存在的话)。你必须使用

   const location = useLocation();
   //For full URL, you have to use location.pathname + location.search
   //For just the path name :- location.pathname
   const redirect = location.pathname + location.search

【讨论】:

    【解决方案2】:

    const redirect = [...searchParam][0] ? /${[...searchParam][0][1]}:'/账户';

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-28
      • 2022-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-21
      • 1970-01-01
      • 2022-06-15
      相关资源
      最近更新 更多