【发布时间】:2015-11-17 16:50:21
【问题描述】:
我正在尝试为模式编写反应组件并在调用 openModal() 时收到错误:“未捕获的 TypeError:this.setState 不是函数”。以下是我的代码:
class Header {
constructor() {
this.state = { showModal: false };
}
openModal() {
this.setState({showModal: true});
}
closeModal() {
this.setState({showModal: false});
}
render() {
return (
<div className="Header">
<ModalDialog heading="heading" show={this.state.showModal}>
<p>Body</p>
</ModalDialog>
<div className="Header-container">
<Navigation className="Header-nav" />
<div className="Navigation2">
<Button bsStyle='primary' onClick={this.openModal.bind(this)}>Sign in</Button>
<Button bsStyle='info' href="/register" onClick={Link.handleClick}>Sign up</Button>
</div>
</div>
</div>
);
}
}
class ModalDialog {
render() {
if (this.props.show) {
return (
<div className="Overlay">
<Modal heading={this.props.heading}>
<div>{this.props.children}</div>
</Modal>
</div>
);
}
return null;
}
};
class Modal {
render() {
return (
<div className="Modal effect">
<h3>{this.props.heading}</h3>
<div className="content">
{this.props.children}
</div>
</div>
);
}
};
我还试图将一个类名从 Header 组件传递到元素 <div className="Modal effect"> 以更改 Modal 的尺寸,这可能吗?
如果有人能帮我解决这个问题,那就太好了。我刚刚开始使用 React。谢谢
【问题讨论】:
-
我设法修复它,必须在代码上方添加 super()。但是关于将类传递给组件。有解决办法吗?谢谢
标签: javascript css reactjs