我有以下ui组件:
...
import stylePropType from 'react-style-proptype';
const Heading = ({
...
marginBottom,
strong
}) => (
<Header
style={{
marginBottom,
fontWeight: strong ? 'bold' : 'normal',
}}
>....
</Header>
);
Heading.defaultProps = {
children: <div />,
marginBottom: 4,
strong: false,
style: {},
};
Heading.propTypes = {
children: PropTypes.node,
marginBottom: PropTypes.number,
strong: PropTypes.bool,
style: stylePropType,
};
如何将当前样式逻辑(marginBottom和fontWeight)与作为道具传入的可选附加样式相结合?有没有办法将两者合并或合并?
答案 0 :(得分:5)
您可以在传入的样式中使用扩展语法(...
):
<Header
style={{
marginBottom,
fontWeight: strong ? 'bold' : 'normal',
...this.props.style
}}
>....
</Header>