我正在尝试将antd表创建为react类而不是组件,尽管我遇到了语法错误。
Line 170: 'state' is not defined no-undef
Line 179: 'onInputChange' is not defined no-undef
Line 183: 'onSearch' is not defined no-undef
Line 232: 'renderTable' is not defined no-undef
Line 283: 'renderTable' is not defined no-undef
我正在尝试导入“自定义过滤器面板”演示 https://ant.design/components/table/#components-table-demo-custom-filter-panel
我是否需要将内容放入此类的构造函数中?使用不同的函数表示法而不是箭头?
var ProfileUserTable = React.createClass({
render: function () {
state = {
filterDropdownVisible: false,
data,
searchText: '',
filtered: false,
};
onInputChange = (e) => {
this.setState({ searchText: e.target.value });
}
onSearch = () => {
const { searchText } = this.state;
const reg = new RegExp(searchText, 'gi');
this.setState({
filterDropdownVisible: false,
filtered: !!searchText,
data: data.map((record) => {
const match = record.name.match(reg);
if (!match) {
return null;
}
return {
...record,
name: (
<span>
{record.name.split(reg).map((text, i) => (
i > 0 ? [<span className="highlight">{match[0]}</span>, text] : text
))}
</span>
),
};
}).filter(record => !!record),
});
}
const data = [{
key: '1',
name: 'John Brown',
age: 32,
address: 'New York No. 1 Lake Park',
}, {
key: '2',
name: 'Joe Black',
age: 42,
address: 'London No. 1 Lake Park',
}, {
key: '3',
name: 'Jim Green',
age: 32,
address: 'Sidney No. 1 Lake Park',
}, {
key: '4',
name: 'Jim Red',
age: 32,
address: 'London No. 2 Lake Park',
}];
renderTable = () => {
const columns = [{
title: 'Name',
dataIndex: 'name',
key: 'name',
filterDropdown: (
<div className="custom-filter-dropdown">
<Input
ref={ele => this.searchInput = ele}
placeholder="Search name"
value={this.state.searchText}
onChange={this.onInputChange}
onPressEnter={this.onSearch}
/>
<Button type="primary" onClick={this.onSearch}>Search</Button>
</div>
),
filterIcon: <Icon type="smile-o" style={{ color: this.state.filtered ? '#108ee9' : '#aaa' }} />,
filterDropdownVisible: this.state.filterDropdownVisible,
onFilterDropdownVisibleChange: (visible) => {
this.setState({
filterDropdownVisible: visible,
}, () => this.searchInput.focus());
},
}, {
title: 'Age',
dataIndex: 'age',
key: 'age',
}, {
title: 'Address',
dataIndex: 'address',
key: 'address',
filters: [{
text: 'London',
value: 'London',
}, {
text: 'New York',
value: 'New York',
}],
onFilter: (value, record) => record.address.indexOf(value) === 0,
}];
return <Table columns={columns} dataSource={this.state.data} />;
}
return (
<div className="component-container">
{renderTable()}
</div>
);
}
});
ReactDOM.render(<ProfileUserTable/>, document.getElementById("app"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="app"/>
答案 0 :(得分:1)
是的,你遗失了很多东西。
该演示使用带箭头功能的ES6 React类,因此您需要babel和transform-class-properties插件或stage-2
要使它与React.createClass一起使用,您需要使用ES5将演示代码转换为正确的createClass样式。例如:
Line 170: 'state' is not defined no-undef
表示你没有正确设置state
状态对象不应该进入渲染,它应该到getInitialState()
Line 179: 'onInputChange' is not defined no-undef
函数onInputChange用箭头函数声明,你应该转换为普通的js函数。
例如
var SayHello = createReactClass({
getInitialState: function() {
return {message: 'Hello!'};
},
handleClick: function() {
alert(this.state.message);
},
render: function() {
return (
<button onClick={this.handleClick}>
Say hello
</button>
);
}
});
这个问题更多是关于与es5 vs es6的反应我建议你阅读他们的不同之处: https://reactjs.org/docs/react-without-es6.html
https://toddmotto.com/react-create-class-versus-component/#state-differences