這是我的jsfiddle例如:https://jsfiddle.net/0se06am5/分頁工作後第二次點擊
class Pagination extends React.Component {
constructor(props) {
super(props);
this.state = { current: 1 };
this.items = ['a', 'b', 'c'];
this.filteredItems = [];
this.prev = this.prev.bind(this);
this.next = this.next.bind(this);
this.set = this.set.bind(this);
this.filter = this.filter.bind(this);
}
componentWillUpdate() {
this.filter();
}
prev() {
this.setState({ current: this.state.current - 1});
}
next() {
this.setState({ current: this.state.current + 1});
}
set(val) {
this.setState({ current: val });
}
filter() {
this.filteredItems = this.items.filter((i, idx) => {
return (idx + 1) === this.state.current;
})
}
render() {
return (
<div>
{this.filteredItems.map((i, idx) => {
return <span key={`item_${idx}`}>{i}</span>;
})}<br />
<span onClick={this.prev}>prev</span>
<span onClick={this.set.bind(this, 1)}>1</span>
<span onClick={this.set.bind(this, 2)}>2</span>
<span onClick={this.set.bind(this, 3)}>3</span>
<span onClick={this.next}>next</span>
<br /><span>current: {this.state.current}</span>
</div>
)
}
}
當你點擊分頁項目,然後你意識到它不能正常工作。
爲什麼filter()
方法運行遲到?而不是componentWillUpdate()
我應該移動這種方法,或者我應該寫什麼不同?
什麼是不工作? –
您必須在分頁上點擊兩次以獲得正確的過濾項目。 – mwl