【问题标题】:How to get current URL domain for react for server side rendering?如何获取当前 URL 域以响应服务器端渲染?
【发布时间】:2020-12-10 17:51:54
【问题描述】:

如何获取当前 URL 域以响应服务器端渲染?由于在 SSR 期间无法访问窗口对象。 React 路由器位置只给出路径名而不是域。

【问题讨论】:

  • 可以在useEffect(()=> console.log(window.location.hostname))里面访问window

标签: reactjs react-redux react-router


【解决方案1】:

您可以尝试将其添加到您应用的代码库中:

if (typeof window === 'undefined') {
  global.window = {}
}

您可以将其添加到主要是 index.js 的启动文件中

【讨论】:

    猜你喜欢
    • 2020-12-22
    • 2015-05-07
    • 1970-01-01
    • 2021-11-29
    • 1970-01-01
    • 1970-01-01
    • 2017-01-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多