【问题标题】:React TypeScript Change route programmatically with BrowserRouter使用 BrowserRouter 以编程方式响应 TypeScript 更改路由
【发布时间】:2020-01-03 10:35:40
【问题描述】:

我使用BrowserRouter 来管理路由。我有一个函数,当它被调用时,我想在不刷新页面的情况下更改路由。

import React from 'react';
const myPage: React.FC = (props) => {
  function loadPage(event: any) {
    if (event.target.id) {
      window.history.push('/' + event.target.id)
    }
  return (
    <div id='home' onClick={loadPage}><div/>
    <section> {props.children} </section>
  )
...

我得到的错误是Property 'push' does not exist on type 'History'.ts(2339)

注意:路由器是该元素的子元素,并被传递到 props.children 中

【问题讨论】:

    标签: reactjs typescript react-router-dom


    【解决方案1】:

    有效的修复是......

    window.location.href = '/'+event.target.id;
    

    【讨论】:

      【解决方案2】:

      窗口中的历史对象没有功能推送。我建议你使用 react-router-dom 的 history 或 Redirect。

      import React from 'react';
      import { withRouter } from 'react-router-dom';
      const myPage: React.FC = (props) => {
        function loadPage(event: any) {
          if (event.target.id) {
            props.history.push('/' + event.target.id)
          }
        return (
          <div id='home' onClick={loadPage}><div/>
        )
      

      那么最后,你应该先包装你的组件,然后再将它导出到 withRouter 组件中

      export default withRouter(myPage);
      

      这样包装可以通过 props 访问历史记录

      【讨论】:

      • 我收到错误Module '"../../../../../../Users/myComp/Documents/myProject/node_modules/@types/react-router-dom"' has no exported member 'history'.ts(2305)
      • 感谢您的帮助,我得到的更改错误现在是 Property 'history' does not exist on type '{ children?: ReactNode; }'.ts(2339)
      • 你能用 console.log 道具看看那里发生了什么吗?您应该能够看到那里的历史和位置。
      • 好的。它在这里说您的组件的类型是 BrowserRouter ,这在您的代码示例中是不可见的。你不需要在 BrowserRouter 中包装这个组件,因为没有它你可以做你想做的事。我建议你去 react router dom 的官方文档看看你到底需要什么。 reacttraining.com/react-router/web/api
      【解决方案3】:

      如果myPage 组件作为道具传递给&lt;Route&gt; 组件,例如:&lt;Route exact path="/myPage" component={myPage}&gt;,那么您可以使用myPage 组件接收的 React-Router 道具进行导航。

      import React from 'react';
      import { RouteComponentProps } from 'react-router';
      
      const myPage: React.FC = (props:RouteComponentProps) => {
        function loadPage(event: any) {
          if (event.target.id) {
            props.history.push('/' + event.target.id)
          }
        return (
          <div id='home' onClick={loadPage}><div/>
        )
      

      【讨论】:

      • 我收到错误Type '(props: RouteComponentProps&lt;{}, StaticContext, any&gt;) =&gt; Element' is not assignable to type 'FunctionComponent&lt;{}&gt;'. Types of parameters 'props' and 'props' are incompatible. Type '{ children?: ReactNode; }' is missing the following properties from type 'RouteComponentProps&lt;{}, StaticContext, any&gt;': history, location, matchts(2322)
      • @Bill 您可以通过将类型参数添加到React.FC 类型const myPage: React.FC&lt;RouteComponentProps&gt; = (props:RouteComponentProps) =&gt; { 来修复它
      猜你喜欢
      • 2017-09-03
      • 2017-08-25
      • 2015-08-30
      • 1970-01-01
      • 2018-03-11
      • 1970-01-01
      • 2016-05-23
      • 2013-12-02
      • 2018-12-28
      相关资源
      最近更新 更多