Picking the Issue
Since I never deal with bugs for my first and second pull request, I decided to find a bug issue to work for my third pull request. The funny thing is that I just noticed that one of the contributors of my second PR's project
issue-collab listed a bug about
"Page not reloading after clicking next on pagination". The owner of the repo wants someone to share a video/gif about the issue because he not really clear what the bug exactly is. I tried and uploaded the gif of the bug.
Then the owner reproduced it as following:- Click Find Issues
- Scroll to bottom of page and click Next page
- Click Find Issues again (observe it doesn't work)
- Click Find Issues a second time (observe it loads the first page)
I asked him assigned the issue to me and he did it immediately.
Practice the git commands
I checked the requirement of release 0.2, and I am able to work on at least 3 different repos for my PR. That means I can work on this repo twice plus 2 more different repo.
Because I worked on the same repo I did for my second PR, I had the fork to this repo already and I am out of date to the upstream too. I add the upstream repo to my remote and fetched it.
Then I did the fast-forward merge using "git merge upstream/master".
Finding the bug
Before I actually found where is the bug, I never know how easy or how complicated the solution can be. Fortunately, the solution of this bug is much easier than I expect, but it still took quite a while to find the fix the code.
apparently, the bug something related to the page number. It is either the findIssue() function did not reset the page, or the nextPage() function made something wrong. Firstly, I always try to console.log some data out to see the data change in each state.
const preFetchState = {
isEmpty: true,
isFetching: true,
isButtonLocked: true,
};
if (shouldResetPageNum) {
preFetchState.pageNum = 1;
}
this.setState(preFetchState);
const finalUrl = this.createUrl();
console.log(preFetchState);
console.log(finalUrl)
await fetch(finalUrl)
when I console the preFetchState and the finalUrl, I found the page number in each of them is different when clicking findIusse() on the second page.
App.jsx:98 {isEmpty: true, isFetching: true, isButtonLocked: true, pageNum: 1}
App.jsx:99 https://api.github.com/search/issues?q=type:issue+label:react+state:open&sort=created&order=desc&per_page=20&page=2
I checked the createUrl() function, but there seems nothing wrong there. I also stupidly change the preFetchState directly to setState, but also not work. After I check a few other functions, I suddenly realized that that might be the same problem I had that which my ignored feather sometimes works, and sometimes not work --- asynchronous problem. I googled online "is the setState asynchronous or synchronous", and it turns out that my guess is right -- setState is an asynchronous function!
fix the bug
As I said before, the solution of this bug is much easier than I expect after I found out what cause the bug -- setState is an asynchronous function, it called createUrl() before it actually changes the value of this.states. I just easily fix this bug by adding the keyword await in front of "this.setState(preFetchState)". I sent the PR of this one line change, and the owner gladly accepts it.
Links
Comments
Post a Comment