Hacktoberfest -3
Finding an issue for Hacktoberfest has been tough, It took me long enough to find an interesting repository, but luckily, I came across this project called Awesome-Adoption, it is a Pet Adoption web application developed using React. The repository owner requested for a new feature to be added to the web application, initially, each pet detail page only displays a single picture of the pet, my task was to try to implement the ability to view all available photos of the pet by adding two buttons to navigate between photos.
Since I have worked with React library back in the WEB-422 course, I felt pretty confident starting this project, but it didn't take long for me to realize that this something I have never worked with before. Back when I worked with React, class based components were used, but absolutely 0 class component were use in this project, this had me confused, time to learn something new again. After a bit of researching I found the video "Learn useState In 15 Minutes - React Hooks Explained" by Web Dev Simplified, and realized that the project was using functional components instead, and it used state hooks to hold the states of objects.
After making sense of using hooks (useState), I began trying to implement the new feature, I noticed that in the existing code, the pet object is stored in a state variable called "pet" which contains all the photos for the pet along with other information on the current page, and it has a "setPet" state setting function from the hook, the page only displays "{pet.photos[0].large}" which is the first photo within the object. This was difficult to work with, because I couldn't directly work with the pet object's state to navigate between photos. It took me quite a while to come up with a solution, that is to make another state variable using the useState hook, this state variable represent the index of the photo, it is initially set to 0 to display the first image, every time when "previous photo" or "next photo" button is clicked, it will check if the previous or next photo exists, if it does, use "setPhotoIndex" hook function to increment/decrement to change the value of the index variable, and change "{pet.photos[0].large}" to "{pet.photos[index].large}" for the image source, which allows navigation between photos.
Links:
Repo: https://github.com/redxzeta/Awesome-Adoption
Issue: https://github.com/redxzeta/Awesome-Adoption/issues/10
Comments
Post a Comment