【发布时间】:2017-07-10 15:15:00
【问题描述】:
我是 reactjs 新手,我正在尝试使用 antd 的模态功能。但是,当我将基本示例代码合并到我的代码库中时,我遇到了一些错误——我不得不删除冒号、尾随逗号,并且状态变量出现错误。
https://ant.design/components/modal/
import { Modal, Button } from 'antd';
class App extends React.Component {
state = { visible: false }
showModal = () => {
this.setState({
visible: true,
});
}
handleOk = (e) => {
console.log(e);
this.setState({
visible: false,
});
}
handleCancel = (e) => {
console.log(e);
this.setState({
visible: false,
});
}
render() {
return (
<div>
<Button type="primary" onClick={this.showModal}>Open</Button>
<Modal
title="Basic Modal"
visible={this.state.visible}
onOk={this.handleOk}
onCancel={this.handleCancel}
>
<p>Some contents...</p>
<p>Some contents...</p>
<p>Some contents...</p>
</Modal>
</div>
);
}
}
ReactDOM.render(<App />, mountNode);
我的错误是语法错误?我尝试在构造函数中设置状态,但未定义。
client:119 ./src/components/Video/VideoConference.js
Module build failed: SyntaxError: D:/wamp/www/e-profound-react/src/components/Video/VideoConference.js: Unexpected token (631:8)
629 | }
630 |
> 631 | state = { visible: false }
| ^
632 | showModal () {
633 | this.setState({
634 | visible: true,
@ ./src/router.js 35:0-65
@ ./src/index.js
@ multi (webpack)-dev-server/client?http://localhost:8080 webpack/hot/dev-server ./index.js
我的代码看起来更复杂,但看起来像这样。
class VideoConference extends React.Component {
constructor (props) {
super(props)
}
componentWillMount () {
}
componentWillUnmount () {
}
componentDidMount () {
}
state = { visible: false }
showModal () {
this.setState({
visible: true,
})
}
handleOk (e) {
console.log(e)
this.setState({
visible: false,
})
}
handleCancel (e) {
console.log(e)
this.setState({
visible: false,
})
}
render () {
return (
<div>
<Spacers />
<Button type='primary' onClick={this.showModal}>Open</Button>
<Modal
title='Basic Modal'
visible={this.state.visible}
onOk={this.handleOk}
onCancel={this.handleCancel}
>
<p>Some contents...</p>
<p>Some contents...</p>
<p>Some contents...</p>
</Modal>
</div>
)
}
}
【问题讨论】:
标签: reactjs modal-dialog antd