Learn how to build real world applications with React! Much more than an intro, you’ll not only learn React, you’ll also learn how to work with a proven set of powerful tools in the React ecosystem to build scalable, real-world applications. Learn to piece together a proven set of tools, adding each tool as you need them, including React Router v4 for paging, Jest for testing, Redux for state management, ESLint and Flow for code quality …and much more!
Complete Introduction to React v3
0:00:00 - 0:11:45
Brian Holt introduces his Complete Introduction to React course and the agenda. Noting that this is the third version of the course, Brian urges students to be sure they are on the appropriate course page.
0:11:46 - 0:19:11
Yarn Dependency Manager
Before working with React, Brain introduces Yarn, an alternative to the npm client for managing dependencies.
0:19:12 - 0:30:37
Simple React Components
- 0:30:38 - 0:38:24 React Paradigm Comparing to other frameworks, Brian reviews the philosophy regarding the React's approach to building applications. Brian takes questions from students.
0:38:25 - 0:50:05
Factories & Props
Brian introduces Factories which are convenience methods for instantiating the component. Then Brian demonstrates how to add props, which is an argument that allows reusable data to be passed into a component. Brian takes questions from students.
- The code up to this point is located on the v2-1 branch.
- 0:00:00 - 0:11:45 Introduction Brian Holt introduces his Complete Introduction to React course and the agenda. Noting that this is the third version of the course, Brian urges students to be sure they are on the appropriate course page.
0:50:06 - 1:08:22
Starting to introduce tools to work with React, Brian first demonstrates Prettier, which is a code formatter to ensure always consistent styling.
1:08:23 - 1:24:37
While Prettier handles the formatting of the code, Brian introduces ESLint to help enforce the syntax of the code. Specifically, Brain sets up the Airbnb configuration of ESLint due to its popularity.
- 1:24:38 - 1:27:59 ESLint and SublimeText Brian installs ESLint into his code editor, SublimeText.
1:28:00 - 1:30:24
webpack and Babel
After working on setting up tools to help clean code along with a linter, Brian changes focus on working on the compilation and building of the code. Brain demonstrates how to set up webpack and Babel, which packages and transpiles code respectively.
- 1:30:25 - 1:43:24 Configuring webpack Brian creates a webpack.config.js file so he can configure an entry point, output directory, and the specific loaders needed to transpile the application source code. Brian takes questions from students.
- 1:43:25 - 1:53:44 Configuring Babel Since not all browsers support all ES6 features, Brian sets up Babel to transpile the ES6 code to a more compatible ES5 version for older browsers. Brian demonstrates how to configure the Babel preset that will be used in Webpack.
- 1:53:45 - 2:04:41 Setting Up webpack.config.js File Brian creates a webpack.config.js file to configure an entry point, output directory, and the specific loaders needed to transpile the application source code.
- 2:04:42 - 2:10:09 Running webpack from npm Now that all the necessary tools are installed and configured, Brian demonstrates how to run Webpack from npm. He also speeds up the build process by setting Webpack to omit the node_modules directory.
- 0:50:06 - 1:08:22 Prettier Starting to introduce tools to work with React, Brian first demonstrates Prettier, which is a code formatter to ensure always consistent styling.
- 2:27:15 - 2:41:57 Starting a Web App With the setup complete, Brian starts to build out a demo application that provides basic Netflix experience, by focusing first on making the home page.
2:41:58 - 2:50:58
Setting up devServer
Brian sets up the preferences in webpack to set up the webpack dev server, speeds up development by letting you run a local server and serve all your content. Brian takes questions from students.
Routing, Props, & State Management
2:50:59 - 2:59:06
Brian introduces the React Router that configures and manages the different pages in the application. Brian separates out the Landing component and sets up the initial routing for the application.
- 2:59:07 - 3:09:38 HashRouter Brian shows the HashRouter for updating the URL. The HashRouter includes the current route after the “#” in the URL. When pattern-matches the requested route, the application then displays the specified component. Continuing with the demonstration with the HashRouter, Brian adds another route for a newly created search page.
3:09:39 - 3:23:03
Once this is working, Brian transitions from using the HashRouter to using the BrowserRouter. Brian takes questions from students.
- 3:23:04 - 3:33:38 Loading JSON Data Using webpack, Brian loads JSON files into the demo application. Taking the data in the JSON file, Brian converts data into components. Brian takes questions from students.
- 3:33:39 - 3:49:30 Making a ShowCard Component With the JSON data in place, Brian adds the UI for displaying shows. Then Brian makes the ShowCard component and introducing how to pass properties into a component. Brian takes questions from students.
3:49:31 - 3:58:16
PropTypes are a more declarative way to specify properties that are passed to a component. Along with declaring individual properties, each property’s type is included as well. Brian adds the propTypes object to the ShowCard component and explains his use of the shape PropType.
- 3:58:17 - 4:02:35 Using the Spread Operator in JSX Brian explains how to use the ES6 spread operator to pass properties into a component. The spread operator simplifies the syntax making it easier to add/remove properties in the propTypes object. The spread operator also removes the need to wrap the properties in a shape. Brian takes questions from students.
- 4:02:36 - 4:06:08 Key Prop When components are generated from a list of data, React needs a method for keeping track of each individual component. The “key” property allows developers to specify a key value for each component. Typically the primary key or some other unique identifier from the data would be used for the key property.
4:06:09 - 4:22:30
- - http://2ality.com/2016/11/computing-tag-functions.html
- 4:22:31 - 4:25:28 One-Way Data Flow Brian introduces the concept of React's one-way data flow, which is also called "one-way binding," keeps everything modular and fast.
4:25:29 - 4:37:12
Q&A: Key and styled-components
Brian takes questions from students about key and styled-components.
- 4:37:13 - 4:53:24 Managing State Brian introduces how the state is managed in a React application. As data is changed inside a component, React has a controlled process of getting, storing, and modifying the current state. The first instance of a mutable state will be in the Search component where Brian adds an input field for the search term.
4:53:25 - 5:08:17
- - https://babeljs.io/docs/plugins/transform-class-properties/
5:08:18 - 5:21:28
- 5:21:29 - 5:34:50 Q&A: Nested Arrays, ES6, & More Brian takes questions from students about route and search component; nested arrays and normalizer; ES6 and arrow functions;
- 2:50:59 - 2:59:06 React Router Brian introduces the React Router that configures and manages the different pages in the application. Brian separates out the Landing component and sets up the initial routing for the application.
5:34:51 - 5:42:31
Snapshot Testing with Jest
Brian introduces Jest that enables snapshot testing. A snapshot test saves the visual look of the code and alerts the developer if the code is modified. Brian begins creating the Search.spec.js file which will run the snapshot test.
5:42:32 - 5:59:39
Configuring & Running Jest
Before running the snapshot test, Brian installs and configures Jest. Part of the configuration includes having Babel transpile the ES6 modules so they can be consumed by Jest. Babel will only transpile these modules when Node is running in a test environment. Brian takes questions from students.
- - https://babeljs.io/docs/plugins/transform-es2015-modules-commonjs/
5:59:40 - 6:09:57
Shallow Testing with Enzyme
In the previous example, Brian modifies the ShowCard component which leads to a test failing in the Search component. Since these are separate components, the tests should not fail. To fix this testing approach, Brian adds Enzyme to his test running. Brian takes questions from students.
- 6:09:58 - 6:18:50 Testing the Number of ShowCards To verify the number of ShowCard components matches the number of shows in the JSON data, Brian writes another unit test. After the test is written, Brian demonstrates how to break the test by including a default search term.
- 6:18:51 - 6:29:04 Testing the Search Field To verify the search field is working properly, Brian uses the test framework to simulate a search term being entered and compares the filtered results to the expected show count.
- 6:29:05 - 6:33:28 Thoughts on Testing Brian offers his perspective on React testing's effectiveness. Brian takes questions from students.
6:33:29 - 6:41:02
Test Coverage with Istanbul
Brian wraps up the section on testing React applications with a demonstration of test coverage. He’s using the Istanbul which creates a code coverage report indicating how much of the source code is covered by a test. Brian also shows the lcov-reports which better visualize the coverage and provide some interactivity.
- 5:34:51 - 5:42:31 Snapshot Testing with Jest Brian introduces Jest that enables snapshot testing. A snapshot test saves the visual look of the code and alerts the developer if the code is modified. Brian begins creating the Search.spec.js file which will run the snapshot test.
Hot Module Replacement
- 6:41:03 - 6:43:50 Introducing Hot Module Replacement Brian introduces a webpack feature called Hot Module Replacement (HMR) that helps replace old modules with the newer ones without stopping the rest of the code from running.
6:43:51 - 7:05:24
Setting up HMR
Brain setups HMR by setting up the babelrc and webpack.config.js. To give hot module reload the ability to split the root component away from what renders the component to the page, Brian then splits the App component out of ClientApp. Brian takes questions from students.
7:08:53 - 7:17:45
Setting up Flow
Brain demonstrates how to set up Flow into the application. Since outside projects are likely to be used in applications might not be written with Flow, Brian installs flow-typed, which is a repository of third-party library interface definitions for use with Flow
- 7:17:46 - 7:34:29 Applying Flow With Flow set up, Brian updates Search.jsx by adding a type for the incoming parameter, which is a synthetic (React) keyboard event. Then Brian gets Flow to be set up in ShowCard.jsx to be typed.
7:34:30 - 7:44:20
Since flow-typed does not have types for Webpack's API, Brian defines his needed types. In the flow-typed folder., Brian adds a file called types.js declares a global variable called module which is an object that has a method called accept. Brian takes questions from students.
7:44:21 - 7:47:36
Fixing flow-typed Definitions
Brian shows how to install the needed definitions for styled-components. Due to the version conflicts with flow-typed, not all the definitions required for the demo application were present during installation.
Data in React
- 7:47:37 - 8:06:42 URL Parameters Brian creates the Details component which will show detailed information about a show. The details component will know which show to display based on data found in the URL. This leads Brian to introduce URL parameters and demonstrate how to specify the parameters in the React Router.
8:06:43 - 8:19:55
Sharing State & Updating the Search Component
The Details component is going to need access to the JSON data. Rather than make a duplicate request for the data, Brian moves the JSON data to the closest common ancestor component. This makes the sharing of state between components easier. Brian finishes some refactoring of the Search component now that it is managing its own state and no longer importing the JSON data.
8:19:56 - 8:27:56
Finishing the Details Component
Brian finishes the Details component by declaring the propTypes object. He then adds the rest of the user interface components which displays the title, year, poster image, description, and the trailer for the selected show.
- 8:27:57 - 8:32:55 Creating a Header Component The header of the application needs to be used across multiple components. Rather than having duplicate code, the header should be extracted out into its own component. Brian removes the header code from the Landing and Search, and Details components and creates a single Header component.
- 8:32:56 - 8:37:26 Conditional Display Logic Part 1 Now that the Header component is being used in multiple places, it will need some conditional logic to determine whether or not the search bar should be shown. Brian begins adding this functionality be creating a boolean propType property named showSearch.
8:37:27 - 8:46:04
Conditional Display Logic Part 2
Brian finishes the conditional display logic in the Header component by using the showSearch propType to determine if the search bar should be displayed. Once he has this code in place, Brian refactors the Search component to use the Header component.
- 8:46:05 - 8:50:12 React Lifecycle Methods Brian introduces the React Lifecycle Methods, which are methods called at different stages throughout a component’s life in an application. One of the most common uses for these methods is to request remote data as soon as a component is mounted. Brian spends a few minutes talking about these methods and to use them.
8:50:13 - 9:09:23
componentDidMount() & AJAX Requests
Brian adds the componentDidMount() lifecycle method into the Details component. He’ll be using this method to trigger an AJAX request to load data for the specified show. Brian is using the axios library to do the AJAX request.
- - https://facebook.github.io/react/docs/react-component.html#componentdidmount
- 9:09:24 - 9:18:52 Showcard Link Spurred by a question from a student, Brian works on polishing the demo application to link from the main page to the details page.
9:18:53 - 9:35:36
Wrapper and Q&A
Taking questions from students, Brian wraps up the main section about React.
9:35:37 - 9:42:56
React Development Tools
Since React code is transpiled and bundled into a single file, debugging React code can be a challenge. Brian spends a few minutes demonstrating React Developer Tools browser plugin to illustrate how to debug React more easily by inspecting the component code.
9:42:57 - 9:58:41
React Performance Tools
Brian shows how to import the React perf tools that automatically hooks into React instance and track wasted renders. Since this approach is to profile React code only, the perf tools is temporary code that should not ship this in production.
- 9:58:42 - 10:05:45 Q&A: Defining Components, & More Brian takes questions from students about when to define components; storing state with context; and more.
- 10:15:50 - 10:32:35 Setting up Redux Brian creates a rootReducer object for the application and explains how the state will be passed to and returned from the reducer. Then Brian creates the store for the application. The createStore method in Redux is passed the rootReducer to create the store. Brian creates the actions for the application. While the files do not need to be in separate files, Brian stores the action definition in an actions.js file and the implementation of the action in an actionCreators.js file. Actions trigger the reducer to carry out the modification of state.
- 10:32:36 - 10:35:36 Connect Redux to React To make landing interact with the store, Brian first connects Redux to React with the react-redux package.
- 10:35:37 - 10:44:09 Landing Interact with Store To set up the Landing.jsx to read and write to Redux, Brian uses Connect function, which allows the component to tap into the Redux store's state. The mapStateToProps allows selects which pieces of state are passed into your component which helps keep thing clean.
- 10:44:10 - 10:50:19 Dispatching Actions to Redux Brian addresses the issue typed text in the input not being sent to Redux. To address this problem, Brian sets actions that are dispatched to Redux using the dispatch() method. With this in place, a component calls dispatch() on its props object and pass the executed action.
10:50:20 - 10:59:01
Brian spends a few minutes walking through the entire Redux integration in the application. After reviewing Redux, Brian refactors the Landing component to map the dispatching of actions to component properties. Brian takes questions from students.
- 10:59:02 - 11:10:40 Search Submit Event Since the search input field in the Landing Component is not navigating to the search page, Brian wraps the input in a Form component to utilize the onSubmit event. Brian takes questions from students.
- 11:10:41 - 11:16:04 Using Redux in the Search Component Brian moves on to the Search component and refactors it to read state from Redux. Once Redux is added, the Search component no longer needs the getInitialState() or handleSearchTermChange() methods because that functionality now exists in Redux.
- 11:16:05 - 11:24:01 Using Redux in the Header Component The last component to refactor is the Header component. Brian imports the connect object from react-redux as well as the setSearchTerm action creator. He then updates the Header component to read the state from Redux and dispatch the action when a new search term is entered.
11:24:02 - 11:28:04
Q&A: Objects and Props, Dispatch, & More
Brian takes questions from students.
11:28:05 - 11:30:54
After taking questions from students before starting, Brian welcomes the class for Day 3 by reviewing past progress
- 11:30:55 - 11:38:53 Refactor Redux for Flow Brian starts to integrate Flux into Redux by adding an ActionTypes, which are types that can only be that string. Brian sets an SET_SEARCH_TERM to an ActionType.
- 11:38:54 - 11:47:49 Refactor Reducers Brian shows how to use combineReducers to create the root reducer. Instead of writing our own root, each reducer separates into its own silo. With combineReducers, each reducer only gets the part that it's concerned about and nothing else.
- 11:47:50 - 11:55:48 Redux DevTools Brian demonstrates the capabilities of the Redux developer tools. Similarly to React, Redux has a set of browser developer tools for viewing and debugging code in the browser. To get the developer tools to work, a small amount of code needs to be added.
11:55:49 - 12:00:07
Q&A: Coding Issue, State, & More
Brian takes questions from students regarding Flow and State, coding issues, and more.
- 12:00:08 - 12:03:07 Middleware & Thunks Brian introduces a thunk, which is a function wrapped around a value and illustrates importance in asynchronous operations.
- 12:03:08 - 12:13:38 Setting Up Middleware & Thunks in Redux Brian adds the redux-thunk library to the application. This middleware will facilitate the exchange between synchronous and asynchronous actions.
- 12:13:39 - 12:24:18 Async Functions Brian creates the getAPIDetails function, which Redux calls to trigger the asynchronous axios AJAX call. The AJAX call results are then dispatched to the addAPIData action.
- 12:24:19 - 12:34:39 Adding Async Actions to the Details Component Brian completes the redux-thunk middleware integration by updating the Details component. The Details component calls the getAPIData() function, which triggers the asynchronous action. Since Redux is being used in the Details component, the application no longer reads the state from the local store.
- 12:34:40 - 12:38:59 Q&A: Async with Redux Brian recaps async with Redux and takes questions from students.
- 12:39:00 - 12:45:19 Updating Snapshot Demonstrating that with the introduction of Redux, Brian shows that most of the existing tests in the application fail because Redux is injected into the components. Brian explains how to use “unwrapped” components in the tests that are free of the Redux dependency. Then Brian updates the snapshot to include the new Header component implementation.
12:45:20 - 12:55:27
Next Brian updates the tests to utilize the Redux store and actions. He wraps the Search component in a Provider so it can have access to the store. He also updates the setting of the search term, so it dispatches a Redux action.
- 12:55:28 - 13:04:05 Testing Reducers Brian creates a reducers.spec.js file for testing the reducers. Since the Redux developer tools can write the test for you, Brian demonstrates how easy it is to write a test for reducers by creating a couple of tests for the reducers using the Redux developer tools. Brian takes questions from students.
13:04:06 - 13:10:59
By first creating new spec called actionCreators.spec.js, Brian demonstrates how to test actionCreators with snapshots.
13:11:00 - 13:24:12
Since handling asynchronous behavior and mock out AJAX requests to test thunks is difficult, Brian introduces moxios, which is a helper for axios in testing.
- 13:24:13 - 13:25:16 Q&A: Tests and Databases Brian answers questions from students about tests and reading data from databases.
- 13:30:42 - 13:34:01 Separating the Client and Server Code For Universal Rendering to work, the application must run both on the server-side and client-side. Brian spends a few minutes separating the client and server aspects of the application with the core logic of the application now residing in an App.jsx file.
- 13:34:02 - 13:46:08 Implementing Server-Side Rendering To compile the server-side code to CommonJS modules, Brian updates the .babelrc configuration file and then adds the server logic. Brian introduces Express and implements the server-side rendering of the application.
13:46:09 - 13:58:03
Running the Node Server
13:58:04 - 14:05:56
With server-side rendering functional, Brian notes that the hot module reload is not working. To address this problem, Brian edits the webpack config to look for the webpack middleware instead of the dev server. Then to make the server work as well, Brian runs the NODE_ENV=server node server.js. Then Brian modifies the dev command in package.json to use the server instead of webpack-dev-server.
Webpack Code-Splitting and Async Routing
- 14:05:57 - 14:08:26 Code Splitting with Webpack Brian introduces a feature of Webpack called code splitting, which gives developers the ability to separate application code into multiple bundles. As a user navigates the application, additional bundles are loaded on-demand.
- 14:08:27 - 14:17:44 Async Routing Brian sets up an async route, which is a higher-order component that displays a loading state until a component is completely loaded. Once the targeted component is loaded, the AsyncRoute will remove the loading state and display the component.
- 14:17:45 - 14:19:35 Setting Up Import To enable Babel, Webpack, and Node to all understand the import(…) syntax, Brian includes a few more plugins in babelrc: one plugin for Babel to understand import at all and a plugin to transform Webpack to know to split.
- 14:19:36 - 14:27:07 Creating a Landing Bundle Brian demonstrates how the Landing bundle is only loaded when the user navigates to the Landing page.
14:27:08 - 14:35:47
Creating Search and Details Bundles
Brian finishes creating the Search and Details code bundles. Brian takes questions from students.
14:35:48 - 14:51:51
Building for Production
Brian demonstrates how to build the application for production by using the Webpack “-p” flag as well as the Webpack UglifyJs Plugin. To showcase the file savings, Brian compares the production files size with the original application size.
14:51:52 - 15:00:29
Brian introduces Preact, which is a near-drop-in replacement for React with a much smaller footprint. Preact can achieve this smaller size by eliminating some legacy browser support and turning over more work to the browser native APIs. Brian refactors the application to use Preact and compares the size of production to the version that used React.
Wrapping Up Introduction to React v3