【问题标题】:How do I export a TypeScript React component which uses Material-UI withStyles?如何导出使用 Material-UI withStyles 的 TypeScript React 组件?
【发布时间】:2019-04-04 09:13:36
【问题描述】:

我有一个用 TypeScript 编写的 React 组件,它将 Material-UI 样式应用于 react-select,如下所示。

const styles = (theme: Theme) => createStyles({
});

export interface Props<TI> extends WithStyles<typeof styles, true> {
    innerProps: Partial<AsyncSelectProps<TI>>;
    selectRef?: React.RefObject<StateManager<TI>>;
    onSelectionChange?: (selection: TI | undefined) => void;
}

class MaterialReactSelect<TI> extends React.PureComponent<Props<TI>> {
    ...
}

export default withStyles(styles, {withTheme: true})(MaterialReactSelect);

我如何修改它,以便将其导出为参数化类型,以便用作

<MaterialReactSelect<string>
    selectRef={this.selectRef}
    onSelectionChange={this.onSelectionChange}
    innerProps={innerProps}
/>

而不是

<MaterialReactSelect
    selectRef={this.selectRef}
    onSelectionChange={this.onSelectionChange}
    innerProps={innerProps}
/>

【问题讨论】:

    标签: reactjs typescript material-ui


    【解决方案1】:

    不幸的是,这不是 material-ui 的问题,而是一般的高阶组件。使用高阶组件时,您将丢失被包装组件的所有泛型类型信息。

    您应该能够从react-reduxstyledstyled-components 观察到相同的问题。

    【讨论】:

    • 是不是说明当前版本的TypeScript没有解决方案?
    • 据我所知。至少不是高阶组件。使用带有钩子的平面组件不会有这些问题。
    • 您有使用 Material-UI 作为带有样式挂钩的平面组件的示例吗?
    • @JonFreedman v4 有一个 withStyles 的钩子变体,记录在 next.material-ui.com/styles/basics/#getting-started
    • 谢谢 - 看来我会等待 v4 发布然后
    猜你喜欢
    • 1970-01-01
    • 2020-06-24
    • 1970-01-01
    • 2020-06-21
    • 2019-10-06
    • 1970-01-01
    • 2019-06-14
    • 2018-11-06
    • 1970-01-01
    相关资源
    最近更新 更多