import { Table, Input, Icon, Button, Popconfirm } from 'antd';
class EditableCell extends React.Component {
state = {
value: this.props.value,
editable: false,
}
handleChange = (e) => {
const value = e.target.value;
this.setState({ value });
}
check = () => {
this.setState({ editable: false });
if (this.props.onChange) {
this.props.onChange(this.state.value);
}
}
edit = () => {
this.setState({ editable: true });
}
render() {
const { value, editable } = this.state;
return (
<div className="editable-cell">
{
editable ?
<div className="editable-cell-input-wrapper">
<Input
value={value}
onChange={this.handleChange}
onPressEnter={this.check}
/>
<Icon
type="check"
className="editable-cell-icon-check"
onClick={this.check}
/>
</div>
:
<div className="editable-cell-text-wrapper">
{value || ' '}
<Icon
type="edit"
className="editable-cell-icon"
onClick={this.edit}
/>
</div>
}
</div>
);
}
}
class EditableTable extends React.Component {
constructor(props) {
super(props);
this.columns = [{
title: 'name',
dataIndex: 'name',
width: '30%',
render: (text, record) => (
<EditableCell
value={text}
onChange={this.onCellChange(record.key, 'name')}
/>
),
}, {
title: 'age',
dataIndex: 'age',
}, {
title: 'address',
dataIndex: 'address',
}, {
title: 'operation',
dataIndex: 'operation',
render: (text, record) => {
return (
this.state.dataSource.length > 1 ?
(
<Popconfirm title="Sure to delete?" onConfirm={() => this.onDelete(record.key)}>
<a href="#">Delete</a>
</Popconfirm>
) : null
);
},
}];
this.state = {
dataSource: [{
key: '0',
name: 'Edward King 0',
age: '32',
address: 'London, Park Lane no. 0',
}, {
key: '1',
name: 'Edward King 1',
age: '32',
address: 'London, Park Lane no. 1',
}],
count: 2,
};
}
onCellChange = (key, dataIndex) => {
return (value) => {
const dataSource = [...this.state.dataSource];
const target = dataSource.find(item => item.key === key);
if (target) {
target[dataIndex] = value;
this.setState({ dataSource });
}
};
}
onDelete = (key) => {
const dataSource = [...this.state.dataSource];
this.setState({ dataSource: dataSource.filter(item => item.key !== key) });
}
handleAdd = () => {
const { count, dataSource } = this.state;
const newData = {
key: count,
name: `Edward King ${count}`,
age: 32,
address: `London, Park Lane no. ${count}`,
};
this.setState({
dataSource: [...dataSource, newData],
count: count + 1,
});
}
render() {
const { dataSource } = this.state;
const columns = this.columns;
return (
<div>
<Button className="editable-add-btn" onClick={this.handleAdd}>Add</Button>
<Table bordered dataSource={dataSource} columns={columns} />
</div>
);
}
}
ReactDOM.render(<EditableTable />, mountNode);
以上是ant design的一个表格 的demo,我拿来直接用的时候,handleChange = (e) => {}这一类的写法全都报错,我都改成了handleChange(e){},虽然能解决问题,但我不知道原因是什么,大佬解释一下。
问题一:为什么handleChange = (e) => {}这个写法报错
而且我使用这个demo的时候,EditableCell组件的
state = {
value: this.props.value,
editable: false,
}
这里会报错。错误如下
./src/components/edgeTable/edgeTable.js
Module build failed: SyntaxError: Unexpected token (5:10)
3 |
4 | class EditableCell extends React.Component {
> 5 | state = {
| ^
6 | value: this.props.value,
7 | editable: false,
8 | }
@ ./src/components/setiot/setiot.js 23:17-53
@ ./src/components/home.js
@ ./src/index.js
@ multi ./node_modules/_webpack-dev-server@2.9.1@webpack-dev-server/client?http://localhost:8080 ./src/index.js
msgErrors @ client?de60:157
onmessage @ socket.js?1928:38
EventTarget.dispatchEvent @ eventtarget.js?86b6:51
(anonymous) @ main.js?db74:274
SockJS._transportMessage @ main.js?db74:272
EventEmitter.emit @ emitter.js?ec2a:50
WebSocketTransport.ws.onmessage @ websocket.js?75af:35
大佬们帮忙看看
问题二:为什么state报错,我尝试放进construct里面还是报错,报错的内容是this.props.value.
求解决方案
.babelrc这个文件是这样配置的吗?
问题二 this.state = {} 要写在super()后面, 才会有this,这样写: