Cover
Title Page
Copyright and Credits
About Packt
Contributors
Table of Contents
Preface
Chapter 1: TypeScript Basics
Technical requirements
Understanding the benefits of TypeScript
Catching coding errors early
Better developer experience and productivity
Using future JavaScript features
Understanding basic types
Primitive types
Type annotations
Type inference
Any
Void
Never
Enumerations
Objects
Arrays
Creating interfaces, types aliases, and classes
Interfaces
Properties
Method signatures
Optional properties and parameters
Readonly properties
Extending interfaces
Type aliases
Classes
Basic classes
Implementing interfaces
Constructors
Extending classes
Abstract classes
Access modifiers
Property setters and getters
Static
Structuring code into modules
Module formats
Exporting
Importing
Default exports
Configuring compilation
Common options
--target
--outDir
--module
--allowJS
--watch
--noImplicitAny
--noImplicitReturns
--sourceMap
--moduleResolution
tsconfig.json
Specifying files for compilation
TypeScript linting
Installing TSLint
Configuring rules
Built-in rules
Excluding files
Code formatting
Summary
Questions
Further reading
Chapter 2: What is New in TypeScript 3
Technical requirements
Tuples
JavaScript rest and spread syntax
Open-ended tuples
Tuple function parameters
Spread expressions
Empty tuples
Optional tuple elements
The unknown type
Type checking with a type predicate
Type narrowing with a type assertion
Project references
Setting up an example
Referencing projects
Additions to compiler options
Cross-project Go to Definition
Build mode
Default JSX properties
Summary
Questions
Further reading
Chapter 3: Getting Started with React and TypeScript
Technical requirements
Creating a React and TypeScript project
Using create-react-app
Creating a project manually
Creating our folder structure
Creating package.json
Adding TypeScript
Creating tsconfig.json
Adding TSLint
Adding React with types
Creating a root web page
Creating a simple React component
Adding webpack
Project folders and files
Creating start and build scripts
Creating a class component
Creating a basic class component
JSX
Component props
Optional props
Default prop values
Handling class component events
Basic event handlers
The this problem
Function props
Class component states
Defining state type
Initializing the state
Changing state
Class component life cycle methods
componentDidMount
componentWillUnmount
getDerivedStateFromProps
getSnapshotBeforeUpdate and componentDidUpdate
shouldComponentUpdate
Deprecated life cycle methods
Creating a function component
Creating a basic function component
Stateful function components
Function component life cycle hooks
Optimizing function component rendering
Summary
Questions
Further reading
Chapter 4: Routing with React Router
Technical requirements
Installing React Router with routing types
Declaring routes
Creating navigation
Using the Link component
Using the NavLink component
Route parameters
Handling not found routes
Implementing page redirects
Simple redirect
Conditional redirect
Query parameters
Route prompts
Nested routes
Animated transitions
Lazy loading routes
Summary
Questions
Further reading
Chapter 5: Advanced Types
Technical requirements
Union types
String literal types
String literal union types
Discriminated union pattern
Type guards
Using the typeof keyword
Using the instanceof keyword
Using the in keyword
Using a user-defined type guard
Generics
Generic functions
Generic classes
Overload signatures
Lookup and mapped types
Summary
Questions
Further reading
Chapter 6: Component Patterns
Technical requirements
Container and presentational components
Compound components
Adding reviews to a product
Creating a basic tab component
Leveraging the compound component pattern
Sharing state with React context
Render props pattern
Completing Tabs with render props
Using render prop for tab headings
Using children prop for tab content
Higher-order components
Adding asynchronous data fetching
Implementing the withLoader HOC
Consuming the withLoader HOC
Summary
Questions
Further reading
Chapter 7: Working with Forms
Technical requirements
Creating a form with controlled components
Adding a Contact Us page
Creating controlled inputs
Reducing boilerplate code with generic components
Creating a basic form component
Adding a basic Field component
Sharing state with React context
Implementing our new ContactUs component
Validating forms
Adding a validation rules prop to form
Tracking validation error messages
Invoking validation rules
Triggering validation rule execution from field
Rendering validation error messages
Form submission
Adding a submit button to the form
Adding a onSubmit form prop
Consuming the onSubmit form prop
Summary
Questions
Further reading
Chapter 8: React Redux
Technical requirements
Principles and key concepts
Principles
Key concepts
Installing Redux
Creating actions
Creating state and action types
Creating actions
Creating reducers
Creating a store
Connecting our React app to the store
Adding the store Provider component
Connecting components to the store
Connecting ProductsPage to the store
Connecting ProductsPage to the loading store state
Adding product state and actions to the store
Adding basket state and actions to the store
Connecting ProductPage to the store
Creating and connecting BasketSummary to the store
Managing state with useReducer
Summary
Questions
Further reading
Chapter 9: Interacting with RESTful APIs
Technical requirements
Writing asynchronous code
Callbacks
Callback execution
Handling callback errors
Promises
Consuming a promised-based function
Creating a promised based function
async and await
Using fetch
Getting data with fetch
Basic GET request
Getting response status
Handling errors
Creating data with fetch
Basic POST request
Request HTTP headers
Changing data with fetch
Basic PUT request
Basic PATCH request
Deleting data with fetch
Using axios with class components
Installing axios
Getting data with axios
Basic GET request
Handling errors
Request HTTP headers
Timeouts
Canceling requests
Creating data with axios
Updating data with axios
Deleting data with axios
Using axios with function components
Summary
Questions
Further reading
Chapter 10: Interacting with GraphQL APIs
Technical requirements
GraphQL query and mutation syntax
Reading GraphQL data
Basic query
Returning nested data
Query parameters
Writing GraphQL data
Using axios as a GraphQL client
Getting a GitHub personal access token
Creating our app
Querying the GraphQL server
Using Apollo GraphQL client
Installing Apollo client
Migrating from axios to Apollo
Adding an Apollo provider
Using the query component to query GraphQL
Adding a repository search component
Implementing the search form
Implementing the search query
Rendering the search result
Implementing a mutation with Apollo
Working with cached data in Apollo
Clearing the caching using refetchQueries
Updating the cache after a Mutation
Summary
Questions
Further reading
Chapter 11: Unit Testing with Jest
Technical requirements
Testing pure functions
Creating a basic pure function test
Understanding Jest watch options
Adding structure to unit test results
Testing components
Creating a basic component test
Improving our tests with react-testing-library
Installing react-testing-library
Removing CSS class references from our tests
Using fireEvent for user interaction
Creating a second test for a valid form submission
Using Jest snapshot tests
Mocking dependencies
Using a mock function in Jest
Mocking Axios with axios-mock-adapter
Getting code coverage
Summary
Questions
Further reading
Answers
Chapter 1: TypeScript Basics
Chapter 2: What is New in TypeScript 3
Chapter 3: Getting Started with React and TypeScript
Chapter 4: Routing with React Router
Chapter 5: Advanced Types
Chapter 6: Component Patterns
Chapter 7: Working with Forms
Chapter 8: React Redux
Chapter 9: Interacting with RESTful APIs
Chapter 10: Interacting with GraphQL APIs
Chapter 11: Unit Testing with Jest
Other Books You May Enjoy
Index