This tutorial is out of date and no longer maintained.
Building applications with React can be overwhelming even after you’ve understood the elegant philosophy behind it. More so, managing large applications with React can be confusing at first. The ecosystem has grown with great libraries to save you some nightmares. But that also makes it difficult at first to figure out which library to use.
In this two-part tutorial, we’ll build and deploy a media library app. The application allows users to search and display images and short videos from external services (Flickr and Shutterstock). It would also allow users to select images and videos for preview.
We will build this application with:
We will be using Yahoo’s Flickr API and ShutterStock API for images and short videos respectively.
This tutorial assumes you have a basic understanding of JavaScript and React. Don’t worry if you have none. We will walk through and build the application from the ground up.
Part 1 of this tutorial would cover basic React setup with create-react-app package
, organizing our project workflow, defining routes, and of course testing it out.
In Part 2, we will be using Redux and its async libraries; we will set it up and then integrate it into our application. Finally, we will deploy our application to Heroku for sharing with our friends. Our application would look thus when we’re done.
Our app will be structured to allow you to either contribute to it or use it as a sample boilerplate for bootstrapping your React/Redux applications.
There are loads of React boilerplate out there to help you get started with React. But we’ll be using create-react-app authored by the Facebook team. It allows you to create React applications with no configuration. create-react-app provides developers with the benefits of a more complex setup out of the box.
Let’s get started…
First, install the package globally:
Then, create the media-library
application:
Bam. Our React basic setup is complete with scripts to start, build, and eject. Take a look at your package.json
.
Let’s test it out.
Now, we can structure our project directory and add other dependencies.
Then, remove the default sample app:
Media-library
- public
- favicon.ico
- index.html
- src
- Api
- api.js
- actions
- mediaActions.js
- common
- Header.js
- components
- HomePage.js
- PhotoPage.js
- VideoPage.js
- constants
- actionTypes.js
- containers
- App.js
- MediaGalleryPage.js
- reducers
- imageReducer.js
- index.js
- initialState.js
- videoReducer.js
- sagas
- mediaSaga.js
- index.js
- watcher.js
- styles
- style.css
- store
- configureStore.js
- routes.js
- index.js
- package.json
If the project directory looks verbose, just be patient, and let’s walk-through. The intent of the project structure is to allow you to extend the application’s functionality beyond this tutorial. This would help you stay organized moving forward.
Note: If you’re new to Redux, I recommend Lin Clark’s article on A Cartoon Intro To Redux..
What the heck is happening up there?
When the store receives an updated state, it transmits to the view layer to be rerendered.
Now that we understand the workflow, let’s dive into coding.
Link allows you to navigate to different routes in your application.
IndexLink is the same as Link with the exception of OnlyActiveOnIndex prop set on it.
App component is the parent component of our app. Every other component is a child to it. this.props.children is where other child components are rendered.
We will implement the library route and the component that maps to it in Part 2 of this tutorial.
You would notice that for Header and HomePage components, we’re using stateless functional component. This approach allows us to separate our presentational components from the container components.
It’s a good practice as it enforces functional composition and component reusability. Whereas container components are responsible for your business logic and connecting with the store, presentational components are responsible for the look of your view.
Simply put, presentational components are components whose purpose in life is to render values to the DOM. Container components also known as smart components provide props and behavior to presentational components.
Let’s wire up our project routes.
Now let’s add the entrance to our application - index.js.
We pass in our routes and browserHistory as props to Router here. browserHistory uses your browser’s History API to create a clean and real URL without the gibberish that comes with using hashHistory. hashHistory has its use case, though.
Router is a high-level API that keeps your UI and URL in sync. It ensures that required props are passed whenever you change URL.
ReactDOM is the API for mounting our application on the DOM node(root, in our own case).
Two more things before we test our app.
Add a bootstrap link to a CDN in our public/index.html
.
Let’s add some custom styling.
Let’s test our app now…
Navigate to http://localhost:3000
on your browser.
Bam!!! We’re up again
Building application with React gets better as you understand the flow. In this part, we did:
In the second part of this tutorial, we will be exploring the power of Redux, Redux-saga, and separating our state management system from the React components for scalability and maintainability.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!