-
Notifications
You must be signed in to change notification settings - Fork 625
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Active Page Number Not Reset #198
Comments
I'd like to fix this. Kindly guide me how to go with the code. |
Seems you need to switch the page number from uncontrolled to controlled. In the first case you only indicate the initial page (with the This imply you manage yourself the For e.g.: import ReactPaginate from 'react-paginate';
class PaginatedList extends React.Component {
state = {
pageNumber: 0,
results: [],
searchInput: ''
}
fetchResults () {
const results = []; // fetch ... using this.state.searchInput as filter.
this.setState({ results });
}
render () {
return (
<div>
<input type="text" onChange={(e) => { this.setState({ searchInput: e.target.value }); }} />
<button onClick={this.fetchResults.bind(this)}>Search</button>
// Results here...
<ReactPaginate
pageCount={this.state.results.length}
forcePage={this.state.pageNumber}
onPageChange={(pageNumber) => { this.setState({ pageNumber }); }}
// ...
/>
</div>
);
}
} |
Look into source code: componentWillReceiveProps(nextProps) {
if (typeof(nextProps.forcePage) !== 'undefined' && this.props.forcePage !== nextProps.forcePage) {
this.setState({selected: nextProps.forcePage});
}
} We can know that if the forcePage number still the same, the page will re-render, but won't change. So when you do your search function, you should reset the forcePage number. In my case, I reset the forcePage number by comparing request payload( when do search function, my payload will change): class Pagination extends Component {
constructor(props) {
super(props)
this.state = {pageNumber: 0}
}
componentWillReceiveProps(nextProps) {
let pre = JSON.stringify(this.props.payload)
let next = JSON.stringify(nextProps.payload)
if (pre !== next) {
this.setState({pageNumber: 0})
}
}
render(){
let {pageNumber} = this.state
return(
<ReactPaginate previousLabel={"<"}
nextLabel={">"}
breakLabel={<a className="skip" href="">...</a>}
breakClassName={"break-me"}
pageCount={pageCount}
marginPagesDisplayed={pagesDisplayed}
pageRangeDisplayed={rangeDisplayed}
onPageChange={this.handlePageClick}
forcePage={pageNumber}/>
)
}
} |
@Detachment Doesn't work in all cases. If i do It's just really disappointing to see an open source library with 200k weekly downloads be both designed poorly and not functioning properly. |
Hi @iway1, do you have a MWE to reproduce the issue? The only concurrent case I see right now in re-reading the code is providing an See
That's certainly deceiving - certainly, also, that can be fixed. Anyway the number of downloads is no proof of good design. |
Good work around, thank you! For those wondering I just wrapped the components in a div with a changeable key.
And then update that state when I need to remount and go back to the first page.
Thanks! |
Great idea! |
Hi i use this to implement "Search Function" on my app
but it won't reset the active number to first page whenever i request new chunk of data
For example:
On First data loading i have 10 pages and then i move to second page,
the active class is moved to second page.
after that i do a search request and get 5 pages, the pageCount is reset,
but the active class is still on the second page, not reset to first page
This is the version i use
"react-paginate": "^5.1.0"
Thank you
The text was updated successfully, but these errors were encountered: