2 回答
TA贡献1777条经验 获得超10个赞
快速修复:
在StyledButton.js:
render() {
const {
content,
icon,
iconPosition,
onClick,
type,
...otherProps // take base props passed through wrapper
} = this.props;
// ...
return (
<ButtonToDisplay
{...otherProps} // spread it firstly here so below props can override
onClick={onClick}
content={content}
/>
);
}
为什么有效:
如您所见,styled(comp)''我们用来设置组件样式的语法实际上是一个底层的 HOC 组件,它接收一个组件并返回另一个组件。
因此,当您制作在 astyled component和之间进行拦截的包装器时real component,您需要允许props库生成的包装器通过该包装器。
TA贡献1871条经验 获得超8个赞
您...在破坏时忘记了(扩展运算符)this.props
export default class StyledButton extends React.Component {
render() {
// added ... (spread operator)
const {type, ...additionalProps} = this.props
if (type === 'normal') return <NormalButton {...aditionalProps} />
else if (type === 'action') return <ActionButton {...aditionalProps} />
}
}
这里发生的事情是styled-component在styleprop中传递样式,但是如果没有扩展运算符,你不会传递它,你只是得到一个名为additionalProps.
添加回答
举报
