jon*_*son 3 typescript reactjs
我正在尝试在Typescript中创建和编译React Higer Order组件。
但是我得到一个错误。首先,VSCode对此抱怨
'WrappedComponent' refers to a value, but is being used as a type here.
Run Code Online (Sandbox Code Playgroud)
**并且我的Typescript编译器会引发此错误。**
lib/auth.ts:44:30 - error TS1005: '>' expected.
44 return <WrappedComponent {...this.props} />;
~
lib/auth.ts:44:47 - error TS1109: Expression expected.
44 return <WrappedComponent {...this.props} />;
~
lib/auth.ts:44:48 - error TS1109: Expression expected.
44 return <WrappedComponent {...this.props} />;
Run Code Online (Sandbox Code Playgroud)
**这是我下面的React HOC组件的代码**
import * as React from 'react';
import Router from 'next/router';
import { NextContext } from 'next';
import nextCookie from 'next-cookies';
import { MOVED_TEMPORARILY } from 'http-status-codes';
interface ComponentExtra extends React.Component, React.SFC {
getInitialProps: Function;
}
export const withAuthSync = (WrappedComponent: ComponentExtra) => class extends React.Component {
static async getInitialProps(ctx: NextContext) {
const token = auth(ctx);
const componentProps = WrappedComponent.getInitialProps && (await WrappedComponent.getInitialProps(ctx));
return { ...componentProps, token };
}
render() {
return <WrappedComponent {...this.props} />;
}
};
export const auth = (ctx: NextContext) => {
const { req, res } = ctx;
const { token } = nextCookie(ctx);
if (req && res && !token) {
res.writeHead(MOVED_TEMPORARILY, { Location: '/signin' }).end();
return;
}
return token;
};
Run Code Online (Sandbox Code Playgroud)
**更新错误是由于文件扩展名不是.tsx而是.ts **
该错误意味着未按如下方式解析JSX语法:
lib / auth.ts:44:30-错误TS1005:'>'
Run Code Online (Sandbox Code Playgroud)44 return <WrappedComponent {...this.props} />;
为此,应将auth.ts重命名为auth.tsx。如果为JSX正确配置了TypeScript,则这是唯一需要的修复程序。
| 归档时间: |
|
| 查看次数: |
1022 次 |
| 最近记录: |