【问题标题】:Passing state by history.push({..}) in TypeScript在 TypeScript 中通过 history.push({..}) 传递状态
【发布时间】:2022-01-08 05:06:23
【问题描述】:

我使用history.push 重定向到我想要的 url,并通过包含的状态将信息传递给下一个路由,如下所示:

const history = useHistory()
history.push({
   pathname: '/someurl/',
   state: {
      foo: bar
   }
})

在 JavaScript 中,这完全符合预期。现在我正在将此文件迁移到 TypeScript,我收到以下错误:

Argument of type '{ pathname: string; state: { foo: bar; }; }'
is not assignable to parameter of type 'To'.

Object literal may only specify known properties, and
'state' does not exist in type 'PartialPath'

出于某种原因,他认为我只传递了一个没有状态的路径名,对吧? 有谁知道如何解决这个问题?

【问题讨论】:

  • 你的意思是pushState 不是push...?
  • 嗯,我想应该是.push 至少我在 js 中工作时是这样做的。
  • history 对象没有push,你有没有添加它的库?
  • 是的,我使用useHistory 访问它,并且返回的历史对象没有pushState
  • 啊!! useHistorywindow.history 不同,所以这可以解释一些事情。 react-router 使用的确定类型是什么版本?

标签: javascript reactjs typescript react-router


【解决方案1】:

尝试修复后解决了同样的问题我发现使用自定义历史记录时会出现问题,例如从'history' 导入历史记录;然后<Router history={history}>...</Router> 所以当我用<BrowserRouter>...</BrowserRouter> 替换它时,问题就消失了。

【讨论】:

  • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
猜你喜欢
  • 2020-08-29
  • 2023-03-05
  • 2016-03-14
  • 1970-01-01
  • 2020-01-31
  • 2016-05-23
  • 1970-01-01
  • 2021-10-30
  • 2019-10-10
相关资源
最近更新 更多