【发布时间】:2017-06-21 16:07:08
【问题描述】:
我正在尝试使用 React 和 Material-UI 制作一个 Dialog 组件。
现在,我有 Material-UI 对话框的传统行为,在类中带有一个可以打开对话框的按钮。但我希望这个组件的行为与 Material-UI 对话框的行为相同。
所以我想改变他的状态并重新渲染调用它的组件,如下所示:
var open = false;
<AuthDialog open={open} />
并使用外部按钮将 open 变量更改为 true。
这是我的代码:
class AuthDialog extends React.Component {
constructor(props, context){
super(props, context);
this.state = {
open: false,
};
};
handleRequestOpen = () => {
this.setState({
open: true,
});
};
handleRequestClose = () => {
this.setState({
open: false,
});
};
render() {
return (
<div>
<RaisedButton children="login" onTouchTap={ this.handleRequestOpen } />
<Dialog open={this.state.open} onRequestClose={this.handleRequestClose}>
// Content
</Dialog>
</div>
);
}
}
我是 JS 的初学者,所以如果我的代码不正确,我也会感谢一些 cmets。
编辑:这是我修改后的代码:
class AuthDialog extends React.Component {
constructor(props, context){
super(props, context);
this.state = {
// State of authBody
};
};
render() {
const { onRequestClose, open } = this.props;
return (
<Dialog open={open} onRequestClose={onRequestClose}>
// Body of the dialog
</Dialog>
);
}
}
在父级中:
closeAuthDialog = () => {
this.setState({
openAuthDialog: false,
});
}
openAuthDialog = () => {
this.setState({
openAuthDialog: true,
});
}
<RaisedButton children="login" onTouchTap={ this.openAuthDialog } />
<AuthDialog open={this.state.openAuthDialog} onRequestClose={this.closeAuthDialog} handleLoginSuccess={ this.handleLoginSuccess } />
【问题讨论】:
标签: javascript reactjs material-ui