【发布时间】:2018-02-14 17:32:35
【问题描述】:
我们正在使用 material-ui v1 来组合 HOC,我们正在使用 recompose 实用程序。问题是我们有 2 个样式对象。一个在组件内部定义,另一个是通用样式对象(在另一个 jsx 样式文件中定义,该文件导出系统范围内使用的 javascript 样式对象)。现在我们需要组合这两个样式对象。有没有办法做到这一点? 用 withStyles 组合 2 个样式对象不起作用。 任何帮助将不胜感激。
这是我们的代码:
import { connect } from 'react-redux';
import { withStyles } from 'material-ui/styles';
import compose from 'recompose/compose';
import { generalStyle } from '../../modules/styles/styles';//external style object
//internal style object
const styleObj = theme => ({
paper: {
padding: 16,
color: theme.palette.text.secondary,
},
});
class myCompo extends React.Component {
//..compo code
}
//composing 2 style objects with withStyles does not work
export default compose(
withStyles({ styleObj, generalStyle }),
connect(mapStateToProps, mapDispatchToProps),
)(myCompo );
//here is how generalStyle is defined in styles.jsx file
const generalStyle = theme => ({
floatR: {
float: 'right',
},
floatL: {
float: 'left',
},
parallelItmes: {
display: 'flex',
alignItems: 'center',
padding: theme.spacing.unit,
paddingLeft: theme.spacing.unit + 10,
},
});
module.exports = {
generalStyle,
};
【问题讨论】:
-
不确定,但也许你可以试试
export default compose( withStyles({ styleObj }), withStyles({ generalStyle }), connect(mapStateToProps, mapDispatchToProps), )(myCompo ); -
感谢 Subham 的回复。我已经尝试过了,它以红色显示此警告消息:警告:Material-UI:提供给 classes 属性的密钥
paper未在 Connect(myCompo) 中实现。 -
您是否有理由不采用
generalStyle并使用扩展运算符(即...generalStyle)将其合并到内部样式对象中?这将有效地允许您在需要的任何地方使用这两种样式。 -
感谢朱尔斯的回复。我尝试传播 gerealStyle,但没有奏效。也许我做错了。这就是我尝试这样做的方式:
const styleObj = theme => ({ ...generalStyle, paper: { //rest of style。你能告诉我正确的方法来回答这个问题吗?
标签: javascript reactjs material-ui