unl*_*udo 8 typescript reactjs react-tsx
我正在将 js 迁移到 ts 并且我的修改后的代码出错:
第 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}/>
}
/>
);
}
Run Code Online (Sandbox Code Playgroud)
如果有人看到这个问题,那就太好了:)。谢谢!
1/ 文件需要有 tsx 扩展名
2/ tsx 语法中的语法也是错误的。我改为这个,现在可以了:
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}/>
}
/>
);
}
Run Code Online (Sandbox Code Playgroud)
现在我有另一个绑定元素“C”的问题,但这是另一回事。
谢谢大家!
| 归档时间: |
|
| 查看次数: |
7698 次 |
| 最近记录: |