React Native (feat. Redux)
This course has been updated! We now recommend you take the React Native, v2 course.
Table of Contents
React Components & React NativeScott introduces both React components and the React Native platform. React components utilize JSX to create encapsulated views and have a well-architected separation of concerns. Since React components are not tied to the DOM, they can be used in the React Native cross-platform development environment.
Exercise: Creating a Todo App with ReactIn this exercise, you will create a web-based Todo application using React. Scott begins the exercise by walking through a few React basic concepts like how to work with JSX, handle events, and store state.
Creating a Todo App Solution Part 1Scott begins walking through the solution to exercise 1. In this first part, he writes the JSX for the Todo application and creates the events for handling user input. The application manages state for the list of todo items as well as the current todo item.
Creating a Todo App Solution Part 2Scott demonstrates a couple alternative ways the Todo application could have been created. Instead of handling the state for the new todo, Scott references the input field directly. He also adds the ability to remove a todo item.
Developing with React Native
Foundational Components of React NativeReact Native adds a number of foundational components that are used in place of traditional HTML components. For example, a <View> component will replace an HTML <div> and a <TextInput> component will replace an <input>.
Exercise: Convert the Todo App to React Native Part 1In this exercise, you will set up a new React Native application from the command line. After generating a new application, Scott explains the file structure and the entry files for each platform. He also runs an Android version of the application and views it view through the Genymotion emulator.
Exercise: Convert the Todo App to React Native Part 2Scott continues walking through the exercise which demonstrates how to move from a React web application to a React Native application. He uses the React Native components in place of the traditional web components and codes the handling of application state. Scott also introduces the TouchableHighlight component and talks about how it differs from a Button component.
Convert the Todo App to React Native Solution Part 1Scott demonstrates the full solution to the exercise which converts the Todo web application to React Native. He creates a solution that will work both on iOS and Android and gets the initial application initialized and running.
Convert the Todo App to React Native Solution Part 2Scott concludes the exercise for converting the Todo application to React Native. He uses native components in place of the web components and uses the React Native event APIs for handling user input. Once he finishes, Scott spends a few minutes answering audience questions.
React Native Developer ToolsBefore moving on to the next exercise, Scott spends some time talking about the developer tools available when creating a React Native application. He explains the difference between live-reloading and hot-reloading. He also demonstrates the features in the JS developer mode in the Android emulator.
Exercise: Debugging & LoggingIn this exercise, you will connect the application to the Chrome debugger and get more familiar with the React Native debugging workflow. You’ll also begin viewing logging information on both iOS and Android.
Styling & Asynchronous Data
Styling in React NativeStyles in React Native are created by using the StyleSheet.create method. This method is passed an object containing each style and their properties. Every component in React Native defaults to a Flexbox display. Most CSS properties are available and use camel-case instead of hyphenation.
Common Styling PropertiesScott spends some time talking through a number common styling properties like width, height, position, padding and margin. While percentage values are not supported, he demonstrates how the Dimensions object can be used to create style values based on the width or height of the screen.
Styling a Specific PlatformIn some cases styles or application logic need to be customized based on the platform or platform version. One way of doing this is to include “.ios” or “.android” in the file name. Another way is to write conditional statements using the Platform.OS or Platform.VERSION properties.
Exercise: Styling the Todo ApplicationIn this exercise, you will use the React Native styling API to style the Todo application
Styling the Todo Application SolutionScott walks through the solution to the styling exercise.
HTTP RequestsJust like in a web application, React Native uses the Fetch API for making external HTTP requests. Scott demonstrates how to use the Fetch API to load JSON data from reddit.com . The data is requested in the componentWillMount lifecycle method and stored in a state property.
Exercise: Saving Todo Items to the ServerIn this exercise, you will use the Fetch API to save todo items to the server. Before starting the exercise, Scott walks through how to install and run a local JSON server that can accept CRUD requests and will save items in a local JSON file.
Saving Todo Items to the Server SolutionScott walks through the solution to the exercise on saving todo items to the server.
Introduction to ReduxRedux is a state solutions that will create a single state store for the application. Actions can then be created to modify the state which then trigger the application to re-render. Scott creates a simple Redux example to demonstrate these concepts and explain the role of reducers in state management.
Adding the Store to the ComponentNow that the store and reducers are created, they must be added to the component. Scott uses the Provider object to pass the store into the top-level component of the application.
Using Redux with Synchronous ActionsIn order to simplify this example, Scott modifies the Reddit application to use synchronous data rather than the asynchronous Fetch API. He then maps the properties from the application to the desired state from Redux.
Mapping Actions to PropertiesThe last step is to map the actions to their corresponding properties. Scott creates an ADD_POSTS action and uses the dispatch object to trigger that action and send they action’s payload.
Audience Questions: Redux Dev Tools, RxJS, and Redux AdvantagesScott begins wrapping up the first day with some audience questions. He shares some thoughts on RxJS vs. Redux and talks about some of the advantages and disadvantages of Redux.
Audience Questions: React/React Native and Facebook, Application ShowcaseScott spends a few more minutes answering audience questions. He talks about the React and React Native big picture and the role Facebook played in the direction and development. He also shares a few real-world applications from the React Native showcase page.
Redux ReviewTo conclude the first day with one more look at Redux. He walks through the existing code to reinforce the immutable nature of reducers and further explain the flow of data through the application.
Sweat Book App OverviewScott begins the second day of the React Native course with a quick overview of the Sweat Book application he will be building. The application allows users to track exercises throughout an entire workout. Exercises are added from a search screen and displayed in the exercise list.
Creating the Store and ReducersBefore starting the first exercise related to the Sweat Book application, Scott spends some time reviewing state management with Redux. Scott demonstrates these concepts on the Todo application. He beings by created a store and the reducers for managing the todo items.
Adding Actions and the ProviderWith the store and the reducers created, Scott adds the actions that will be dispatched as todo items are added. He also copies over some styling so the todos will be rendered correctly. Before moving on, Scott introduces the Provider utility which gives components access to the state.
Connecting Redux to the ComponentScott uses the connect object from the react-redux library to gain access to the store. He then maps the desired actions to their corresponding methods in the component. These methods use the Redux dispatcher to send the action type and payload to the reducers for handling.
Subscribing to the StateOnce the component is connected to the store, it can subscribe to the state by mapping the state object to the corresponding component properties. Scott does this by binding the todos reducer to a property named “todos” inside the component.
Stepping through the Redux CallstackNow that Redux is wired up to the component, Scott sets a few breakpoints and enables the debugger. He steps through each call that’s made between the application and the Redux store. This helps better understand the flow of data from the time a user adds a todo item to the time the store is updated.
Exercise: Creating the Sweat Book Root ComponentIn this exercise, you will initialize the Sweat Book application. You’ll create the root component. You will also prep the Redux portion of the application with the necessary store, reducers, and actions.
Creating the Sweat Book Root Component Solution Part 2Scott continues demonstrating the solution to the first Sweat Book exercise. He explains the actions he created and talks about how they are organized. He also answers a few audience questions about sharing constants between components and global variables.
Routing in React NativeRouting in React Native is still evolving. Scott talks a little about the history of routing in React web applications and the direction it’s heading on Native. For the Sweat Book application, he’ll be using a tabbed navigation system to avoid the need for a complex router.
Using the React Native Scrollable Tab View ComponentScott demonstrates how to use the react-native-scrollable-tab-view component inside the Todo application. The root component is wrapped in a <TabView> and given a tabLabel property. Any subsequent tabs are added within they same <TabView> component.
Exercise: Adding Tab NavigationIn this exercise, you will add the tabbed interface to the Sweat Book application. The Welcome screen is not included in the tabs so the user will navigate from the welcome screen to the tabbed interface.
Adding Tab Navigation SolutionScott walks through the solution to the Tabbed Navigation exercise. He created a boolean variable to indicate wether the workout has started or not for determining if the welcome screen should be shown.
React Native Package ManagerThere are many third party libraries that can be used with React Native. They libraries include native implementations for both iOS and Android. Since each platform has its own dependencies, the React Native Package Manager (rnpm) can be used to maintain these libraries and keep them current within a React Native project.
React Native Linear Gradient LibraryScott uses the React Native Package Manager to install the react-native-linear-gradient library. Even though this library uses RNPM, it installs into the node_modules folder and is used like any other module. After getting the module installed, Scott demonstrates how to import it into the Todo application and apply a linear gradient to the background.
Exercise: Applying GradientsIn this exercise, you will use the React Linear Gradient library to apply gradients to both the home screen and the current workouts screen. For extra credit, you can find a vector icon plugin and add the icons into the application.
Applying Gradients SolutionScott walk through the solution to the Applying Gradients exercise.
UI ComponentsUser Interface components encapsulate their functionality and state. Scott demonstrates how to create the CurrentWorkout component which will occupy the first tab within the <TabView>. He also walks through the other UI components that will need to be created in the Sweat Book application.
Reusable UI ComponentsSince the CurrentWorkout component and the modal window will both have a top bar, Scott decides this should be a reusable UI component so it can be used in both places. He starts by making a Topbar UI component and styling it to display within the CurrentWorkout component. Scott then talks a little about how it will be made to be reusable.
Exercise: Creating a ModalIn this exercise, you will create an ExerciseModal component for adding exercises. This component will reuse the Topbar component and be triggered when the add exercise button is pressed.
Creating a Modal Solution Part 1Scott begins walking through the solution to the Creating a Modal exercise. He has created state properties for both the currentWorkout and the exerciseModal. The currentWorkout property will access the state for the current workout. The exerciseModal property is used to determine the modal’s visibility.
Creating a Modal Solution Part 2Scott continues the demonstration of the Creating a Modal exercise. He talks about how to implement closing the modal. He also sets some breakpoints and steps through the execution of the code to illustrate the changes in state.
TextInput ComponentScott adds a TextInput component into the top bar of the modal to search for exercises. Once that’s in place, he spends a few minutes debugging some other issues in the application and answering some audience questions.
ListView ComponentThe exercise list will be displayed in a ListView component. A ListView component has a datasource which not only provides data, but also specifies an algorithm for determining when the data has changed. Once the ExerciseList component is created, Scott adds it into the modal.
Finishing the Exercise ModalWith all the components, Scott now finishes the exercise modal and implements the search functionality. As a search term is entered, a fuzzy search will be performed and return a list of rows to populate in the ExerciseList component.
Exercise: Adding an ExerciseIn this exercise, you will create a new action and implement the add-exercise functionality. When an exercise in the modal is tapped, it will be added to the currentWorkout array.
Adding an Exercise SolutionScott walks through the solution to Adding an Exercise. He uses a TouchableOpacity component to make the exercises tappable. When the user taps on an exercise an action is dispatched and it is added and displayed in the current workout list.
Redux Thunk MiddlewareIn order for Redux to handle asynchronous actions it requires a piece of middleware. Scott introduces the redux-thunk library and describes how a thunk acts as an asynchronous function that dispatches actions when it resolves. He then implements an async action in the Todo application.
Calling Async ActionsWith the asynchronous actions created, Scott can update the Todo application to call these actions to load and create todo items. Once the application is updated Scott starts the JSON server and tests the async operations in the simulator.
Exercise: Async ReduxIn this final exercise, you will use a utility function called createAsyncCreator to generate the async operations in the Sweat Book application. You’ll also update the application to dispatch the new async actions.
Async Redux SolutionScott walks through the solution to the Async Redux exercise. He also spends somme time explaining why he is using the createAsyncCreator function to generate the async operations.
Audience Questions & ResourcesScott spends the final minutes of the workshop answering some audience questions and sharing a few resources. Scott recommends looking at the React Native Docs for information on testing. He also talks about a few React Native competitors and how their approaches differ.