Introduction
Kicking off the tutorial 'Advanced Network Patterns in React', this chapter sets the stage for exploring diverse network request patterns in React applications. It establishes the foundational knowledge needed for handling complex network scenarios in frontend development.
In this chapter, you will learn
This tutorial is designed to explore a range of patterns for executing network requests in React applications. Although the focus is on React, the principles and challenges discussed are relevant to other frontend libraries and frameworks.
Starting with a basic user profile, the tutorial progressively introduces more complex scenarios. These scenarios are intended to illuminate common problems and solutions encountered in large-scale applications, with a primary aim of demystifying the often-overlooked performance pitfalls in frontend development.
While React serves as the primary example, the patterns and strategies discussed are broadly applicable across various frontend technologies. They offer universal insights that can be valuable in diverse development contexts.
The tutorial assumes you have a foundational understanding of React, including familiarity with JSX and common hooks such as useState
and useEffect
.
Key learning outcomes of this tutorial include:
- Gaining a deep understanding of the challenges inherent in network programming and why it can be difficult to get right.
- Unraveling and demystifying the most commonly misunderstood yet widely used patterns.
- Exploring ways to make asynchronous service calls more manageable and less error-prone.
- Investigating alternative approaches for enhancing user experience.
- Learning how to apply different strategies in both frontend and backend development.
- Looking ahead to the future of server-side work and its implications for frontend development.
This tutorial aims to equip you with the knowledge and tools to navigate the complexities of network requests in React and other frontend frameworks, enhancing both your understanding and practical skills.
The page we're going to build is a Home
page in an imaginary social media website. It doesn't do much but showing a user their home page when then log in.
Setting up the environment
We're going to use vite as the scaffolding tool to generate the structure of the application, use the following command to create a React with TypeScript enabled.
Let's clean up the generated template file, open up the src/App.tsx
, and put the following code:
We are going to use Tailwind Css
for styling in this tutorial.
Tailwind is a utility-first CSS framework packed with classes like flex, pt-4, text-center and rotate-90 that can be composed to build any design, directly in your markup.
Utility-first CSS frameworks provide a comprehensive set of CSS utility classes for common styling tasks. Instead of writing custom CSS, developers can construct designs by combining these utility classes directly in their markup. This approach promotes rapid UI development, consistency across pages, and can lead to more maintainable codebases.
To install Tailwind, go to the react-network-advanced
folder we created above, and execute the following command in Terminal (if you're on Mac OS)
And then you will need to config Tailwind
to allow it scan index.html
and all tsx
files under src
folder.
Lastly, you will need to use @tailwind
directives in src/index.css
to actually enable it:
I prefer to make the background a bit gray, so I'll add the following line in src/index.css
With these changes in place, let's launch the application now in command line:
It by default will launch your React application on "http://localhost:5173/", And in your browser, you should be able to see the text Profile
Setting up the backend service
We are going to call some API endpoints in the course of the tutorial, I have published them into a Github repo, go ahead and clone the repo to your local (assume it's in folder: mock-server-network-react-tutorial
.
Run the following command to launch the mock server:
You would be able to see something like this in your console:
And if you try to access the one of the following API endpoint:
Or if you prefer, you could use jq
to format the output, which is a bit easier to read (curl http://localhost:1573/users/u1 | jq .
). And you should be able to see response like the following:
Ready to get started?
This introductory chapter equips learners with the essential setup and context for tackling advanced network patterns in React, paving the way for more complex concepts and practical applications in subsequent chapters.
The next chapter will dive into using useEffect for building a basic profile page, demonstrating sequential network requests.