我有一个从另一个 React 组件启动的覆盖层,该组件有一个也会改变自身的按钮。当用户单击按钮时发生更改,然后按钮更改其类名。它还向作为覆盖层的子组件发送一个 prop 值。叠加层根据属性和是否单击添加一个类。一切都很好,但现在我必须做另一件事。当用户点击覆盖层时,它必须关闭。在此更改之前,我之前提到的按钮一切正常。
这是按钮组件:
export default class StatusBarButton extends React.Component {
constructor(props) {
super(props)
this.state = {active: false}
this.handleClick = this.handleClick.bind(this)
}
handleClick() {
this.setState({ active: !this.state.active });
}
render() {
var cx = require('classnames');
var button = cx ({
'statusbar-button-container' : true,
'active' : this.state.active
});
var animation = cx ({
'statusbar-button-cross-image' : true,
'rotate' : true,
'active' : this.state.active
});
return (
<div className="astatusbar-center-wrapper">
<div className={button} onClick={this.handleClick}>
<img src="images/navbar-element-icon-cross.png" className={animation}/>
</div>
<OverlayView isOpen={this.state.active} />
</div>
);
}
}
这是目前的叠加层:
export default class OverlayView extends React.Component {
constructor (props){
super(props);
this.state = {open: false}
this.setState({ open: this.props.isOpen });
}
render() {
var cx = require('classnames');
var overlay = cx({
'overlay': true,
'overlay-slidedown': true,
'open': this.state.open
});
return (
<div className={overlay} onClick={this._closeOverlay}>
<div className="overlay-menu-wrapper">
<OverlayNewInvoiceButton />
<OverlayNewBudgetButton />
<OverlayNewTicketButton />
</div>
</div>
);
}
}
如您所见,我正在尝试获取 prop 值并将其分配给状态,但它不起作用。如何将 prop 值分配给 state 并使用它?
问候, JP