React With Rails Api
My Final Flatiron school project is A imgur.com clone which is built using a react-redux front end and ruby on rails backend.
To display my images on the homepage I have to get all the images and there information and add it to the react images state.
I render the index of images information in JSON format.
Now that my rails API is showing me the JSON format I will use my react front end to fetch the information and update my state to reflect the images.
A few things are happening here
- I create a function to call when I need to get the images
- I use the fetch method to get the images information from my API
- I then use response.json() to return the information i received as json format.
- Then I dispatch function SetImages which i created passing in the json formatted images information.
- The setImages function passes the Images JSON data which is then passed to the images reducers which updates it state with A array of all the images.
I call my getImages() function once my images display page renders using componentDidmount life cycle component.
I then map and pass each image from my state to a imagecard stateless component to sort and display the information from my state.
The Final Display