2 回答

TA貢獻(xiàn)1777條經(jīng)驗 獲得超10個贊
快速修復(fù):
在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)''我們用來設(shè)置組件樣式的語法實(shí)際上是一個底層的 HOC 組件,它接收一個組件并返回另一個組件。
因此,當(dāng)您制作在 astyled component和之間進(jìn)行攔截的包裝器時real component,您需要允許props庫生成的包裝器通過該包裝器。

TA貢獻(xiàn)1871條經(jīng)驗 獲得超8個贊
您...在破壞時忘記了(擴(kuò)展運(yùn)算符)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} />
}
}
這里發(fā)生的事情是styled-component在styleprop中傳遞樣式,但是如果沒有擴(kuò)展運(yùn)算符,你不會傳遞它,你只是得到一個名為additionalProps.
添加回答
舉報