简体   繁体   English

反应打字稿错误解析错误:'>'预期

[英]React typescript error Parsing error: '>' expected

I am migrating js to ts and have an error with my modified code:我正在将 js 迁移到 ts 并且我的修改后的代码出错:

Line 26:8: Parsing error: '>' expected第 26:8 行:解析错误:“>”预期

import React from "react";
import { Route, Redirect, RouteProps } from "react-router-dom";
import {render} from "react-dom"
import {AppProps} from "../App"

function querystring(name: string, url = window.location.href) {
  name = name.replace(/[[]]/g, "\\$&");

  const regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)", "i");
  const results = regex.exec(url);

  if (!results) {
    return null;
  }
  if (!results[2]) {
    return "";
  }

  return decodeURIComponent(results[2].replace(/\+/g, " "));
}
export default function UnauthenticatedRoute({ component: C, appProps, ...rest }:
                                                 RouteProps & {appProps: AppProps}) {
  const redirect = querystring("redirect");
  return (
    <Route
        {...rest} // the issue is here
        render={ props => !appProps.isAuthenticated ?
            <C {...props} {...appProps} />
          :
            <Redirect to={redirect === "" || redirect === null ? "/" : redirect}/>
    }
    />
  );
}

If someone sees the problem, that would be kind :).如果有人看到这个问题,那就太好了:)。 Thanks!谢谢!


Solution解决方案

1/ File needs to have tsx extension 1/ 文件需要有 tsx 扩展名

2/ syntax was also wrong in the tsx syntax. 2/ tsx 语法中的语法也是错误的。 I changed to this and now it's ok:我改为这个,现在可以了:

export default function UnauthenticatedRoute({ component: C, appProps, ...rest }:
                                                 RouteProps & {appProps: AppProps}) {
  const redirect = querystring("redirect");
  return (
    <Route {...rest}
        render={ props => !appProps.isAuthenticated ?
            <C {...props} {...appProps} />
          :
            <Redirect to={redirect === "" || redirect === null ? "/" : redirect}/>
        }
    />
  );
}

Now I have another issue with binding element 'C', but it's another story.现在我有另一个绑定元素“C”的问题,但这是另一回事。

Thanks everyone!谢谢大家!

将文件扩展名从.ts更改为.tsx

请注意:返回 react-dom 时,请始终使用 TSX,否则使用 TS

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM