【发布时间】:2017-02-10 22:19:41
【问题描述】:
如何从 ReactJS 组件中获取完整的 URL?
我认为它应该类似于 this.props.location 但它是 undefined
【问题讨论】:
标签: reactjs
如何从 ReactJS 组件中获取完整的 URL?
我认为它应该类似于 this.props.location 但它是 undefined
【问题讨论】:
标签: reactjs
阅读本文我找到了React / NextJs 的解决方案。因为如果我们在 react 或 nextjs 中直接使用 window.location.href 会抛出类似的错误
服务器错误 ReferenceError: 未定义窗口
import React, { useState, useEffect } from "react";
const Product = ({ product }) => {
const [pageURL, setPageURL] = useState(0);
useEffect(() => {
setPageURL(window.location.href);
})
return (
<div>
<h3>{pageURL}</h3>
</div>
);
};
【讨论】:
window.location.href 是您所需要的。但是,如果您使用的是反应路由器,您可能会发现检查 useLocation 和 useHistory 钩子很有用。
两者都创建了一个具有路径名属性的对象,您可以读取这些属性,并且对一堆其他东西很有用。 Here's a youtube video explaining react router hooks
两者都会给你你需要的东西(没有域名):
import { useHistory ,useLocation } from 'react-router-dom';
const location = useLocation()
location.pathname
const history = useHistory()
history.location.pathname
【讨论】:
window.location.href不是React的方式。
只是为了向这个页面添加一点进一步的文档 - 我已经为这个问题苦苦挣扎了一段时间。
如上所述,获取 URL 的最简单方法是通过 window.location.href。
然后我们可以使用 let urlElements = window.location.href.split('/') 来通过 vanilla Javascript 提取部分 URL
然后我们将console.log(urlElements) 查看通过在 URL 上调用 .split() 生成的元素数组。
找到要访问的数组中的哪个索引后,您可以将其分配给变量
let urlElelement = (urlElements[0])
现在您可以使用 urlElement 的值,这将是您 URL 的特定部分,无论您想要什么。
【讨论】:
要获取当前路由器实例或当前位置,您必须使用withRouter 从react-router-dom 创建一个高阶组件。否则,当您尝试访问 this.props.location 时,它将返回 undefined
例子
import React, { Component } from 'react';
import { withRouter } from 'react-router-dom';
class className extends Component {
render(){
return(
....
)
}
}
export default withRouter(className)
【讨论】:
正如其他人提到的,首先你需要react-router 包。但是它为您提供的 location 对象包含已解析的 url。
但是如果你非常想要完整的 url 而不访问全局变量,我相信最快的方法是
...
const getA = memoize(() => document.createElement('a'));
const getCleanA = () => Object.assign(getA(), { href: '' });
const MyComponent = ({ location }) => {
const { href } = Object.assign(getCleanA(), location);
...
href 是包含完整网址的那个。
对于memoize,我通常使用lodash,它的实现方式主要是为了避免不必要地创建新元素。
P.S.:当然,您不受古老浏览器的限制,您可能想尝试new URL() 的东西,但基本上整个情况或多或少毫无意义,因为您以一种或另一种方式访问全局变量。那么为什么不改用window.location.href呢?
【讨论】:
如果您需要 URL 的完整路径,您可以使用 vanilla Javascript:
window.location.href
要仅获取路径(减去域名),您可以使用:
window.location.pathname
console.log(window.location.pathname); //yields: "/js" (where snippets run)
console.log(window.location.href); //yields: "https://stacksnippets.net/js"
来源:Location pathname Property - W3Schools
如果你还没有使用“react-router”,你可以使用:
yarn add react-router
然后在 "Route" 内的 React.Component 中,您可以调用:
this.props.location.pathname
这会返回路径,不包括域名。
感谢@abdulla-zulqarnain!
【讨论】:
window.location.pathname那样做
this.props.location is a react-router feature,如果你想使用它,你必须安装它。
注意:不返回完整的网址。
【讨论】:
您收到 undefined 是因为您可能拥有 React Router 之外的组件。
请记住,您需要确保调用 this.props.location 的组件位于 <Route /> 组件中,例如:
<Route path="/dashboard" component={Dashboard} />
然后在仪表板组件中,您可以访问this.props.location...
【讨论】:
<Route /> 内,您可以使用withRouter 高阶组件。
window.location.href 是您正在寻找的。
【讨论】:
req.originalUrl 之类的东西。支持 SSR 的各种 react 路由库都有获取此信息的方法。