我如何 return 使用 withStyles 的 React 组件?

How can I return a react component using withStyles?

这里的问题不是关于如何导出,而是如何 return 一个带有 css 注入的 React 对象?

我正在努力实现类似的目标:

return ( withStyles(this.props.style)(<Component {...params}/>) );

目的是 return Component 所有 CSS 使用 withStyles 设置及其样式在名为 style.

的 属性 中注入

withStyles HOC 需要 class/function 和 returns 装饰 class/function。这就是为什么我们不能在那里传递组件实例(<Component {...params}> creates/returns 引擎盖下的对象)。

考虑到这一点以及 JSX 要求组件名称以大写字母开头,我们接下来可以做:

const StyledComponent = withStyles(this.props.style)(Component);
return <StyledComponent {...params} />;