你的方法是正确的,但你在这里犯了错误:
SomeComponent.defaultProps = {
backgroundColor = 'grey',
overflow: 'hidden',
};
这样,背景色和溢出将在内部可用
this.props
,不在
this.props.containerStyles
containerStyles
.
SomeComponent.defaultProps = {
containerStyles: {
backgroundColor: 'grey',
overflow: 'hidden',
}
};
或使用:
style={{ ...styles.container, ...this.props }}
工作示例(使用
this.props.containerStyles这个.props.containerStyles
class SomeComponent extends React.Component {
constructor(props) {
super(props);
};
render() {
return (
<div
style={{ ...styles.container, ...this.props.containerStyles }}>
hello
</div>
)
};
};
const styles = {
container: {
width: '50px',
height: '60px',
},
};
SomeComponent.defaultProps = {
containerStyles: {
backgroundColor: 'grey',
overflow: 'hidden',
}
};
const rootElement = document.getElementById("root");
ReactDOM.render(<SomeComponent />, rootElement);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id='root' />
工作示例(使用
这个道具
):
class SomeComponent extends React.Component {
constructor(props) {
super(props);
};
render() {
return (
<div
style={{ ...styles.container, ...this.props }}>
hello
</div>
)
};
};
const styles = {
container: {
width: '50px',
height: '60px',
},
};
SomeComponent.defaultProps = {
backgroundColor: 'grey',
overflow: 'hidden',
};
const rootElement = document.getElementById("root");
ReactDOM.render(<SomeComponent />, rootElement);
<script src=“https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js”></script>
<script src=“https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react dom.min.js”></script>