多个导出的语法错误,包括重命名的 HOC

Syntax error with multiple exports including a renamed HOC

我想从同一个文件导出多个,其中一个是重命名为默认的高阶组件,但我收到语法错误。

//Shape.js
import React, { createContext, memo } from "react";

const ResizeAPI = createContext();

const Shape = (props) => {

    /* ... */
}

export {
    
    memo(Shape) as default, // <-- Syntax/Parsing Error: Unexpected token, expected ","
    ResizeAPI
};

如果我不包装 Shape 组件,则没有语法错误:

export {
    
    Shape as default, // <-- This is fine
    ResizeAPI
};

另外,如果我只导出主要组件,即使它被包装,也没有语法错误:

export default memo(List); // <-- This is fine

如何在导出列表中导出重命名的 HOC?

问题是您导出的对象没有有效的键,但您也不能使用 as default 语法的键,请尝试类似:

// preferable
export default memo(Shape);
export { ResizeAPI };

// or
const MemoShape = memo(Shape);
export { MemoShape as default, ResizeAPI };