Umi: 使用useLocation().query报错

Created on 23 Jul 2020  ·  4Comments  ·  Source: umijs/umi

What happens?

你好,我在typescript中使用useLocation hooks会报错:

Mini Showcase Repository(REQUIRED)

const location = useLocation()
console.log(location.query); // 类型“Location<PoorMansUnknown>”上不存在属性“query”。ts(2339)

另外如果这个问题解决了,读取location.query.id应该也会报错:query上没有id这个属性,应该怎么处理呢?

Context

  • Umi Version: ^3.2.9
  • Node Version: 12.9.1
  • Platform: mac

Most helpful comment

@sorrycc 我刚才扫了一下 umi 的源码,这个好像确实是个 bug。

umi 导出的 HistoryLocation 确实是 DOM 的,从 history-with-query 导出的。但是 useLocation()useHistory() 是直接从 react-router-dom 导出的。

All 4 comments

本来就没有,这个不是 BOM/DOM 对象。see https://github.com/ReactTraining/react-router/issues/7446

对应的文档 API 是:https://reactrouter.com/web/api/location

{
  key: 'ac3df4', // not with HashHistory!
  pathname: '/somewhere',
  search: '?some=search-string',
  hash: '#howdy',
  state: {
    [userDefined]: true
  }
}

@sorrycc 我刚才扫了一下 umi 的源码,这个好像确实是个 bug。

umi 导出的 HistoryLocation 确实是 DOM 的,从 history-with-query 导出的。但是 useLocation()useHistory() 是直接从 react-router-dom 导出的。

+1,目前用 history。
useHistory 类型没有query

Was this page helpful?
0 / 5 - 0 ratings

Related issues

onReadyL picture onReadyL  ·  3Comments

afc163 picture afc163  ·  3Comments

zhanchengkun picture zhanchengkun  ·  3Comments

sorrycc picture sorrycc  ·  4Comments

haiing picture haiing  ·  3Comments