0% completed
What is React?
On This Page
Pages That Change Without Reloading
Server-Side Rendering
Client-Side Rendering
Picture a post on a social media site with the text "0 likes" under it. You click the Like button, and the text changes to "1 like". The page does not go blank, the scroll position stays where it was, and nothing else on the screen moves.
Code that makes a page behave this way has to do two jobs: remember the current number of likes, and change exactly the right piece of text when that number changes. React is a JavaScript library that handles the second job for you and gives you a clear pattern for the first. You describe what the screen should show for the current data, and React updates the page in the browser whenever that data changes.
React is open source. It was created at Facebook, now Meta. Today it is owned by the React Foundation, an independent organization hosted by the Linux Foundation, and it is developed by contributors from Meta and many other companies. You will also see it called React.js or ReactJS; all three names mean the same library.
To follow this course, you need a good understanding of HTML and CSS and, most of all, JavaScript.
Pages That Change Without Reloading
The Like button above is part of a single-page application (SPA). An SPA loads one HTML page and then changes parts of that page with JavaScript as the user works with it. The browser does not ask the server for a new page after every click, so the screen never goes blank between actions.
React is widely used to build SPAs, and that is the use this course teaches. React is not limited to them, however. Frameworks built on top of React, such as Next.js and React Router, can also produce pages on a server. The rest of this lesson explains the two ways a page can reach the screen, because React can take part in both.
Server-Side Rendering
In a traditional website, the server does most of the work. Each time the user opens a page, the server builds the complete HTML for that page and sends it to the browser, a process called server-side rendering (SSR). The diagram below shows the first load of such a page.
Follow the six numbered steps from left to right. The user requests a page, and the server creates a ready HTML file. At step 3, the browser draws that HTML, so the user sees the content quickly, but the buttons do not respond yet. The browser then downloads the page's JavaScript and runs it, and only after step 5 is the page fully interactive. The bar under the steps shows what the user sees while each step happens.
In an older server-rendered site, every later action repeats the whole trip. Clicking a link or submitting a form asks the server for a new page, and the browser replaces the current page with it. On a large page with many small actions, those repeated reloads make the site feel slow.
Client-Side Rendering
Client-side rendering (CSR) moves the work into the browser, which is the client. The server sends a small HTML file that contains little more than links to JavaScript and CSS files. The browser downloads those files and runs the JavaScript, and the JavaScript, using a library such as React, builds the page content.
This diagram has the same six steps and the same bar, so you can compare it with the one above. The user requests a page, the server sends an HTML file with JavaScript links, and the browser downloads the HTML. Next, the browser downloads the CSS and JavaScript and runs the library. At step 6, the library builds the page content, and only then does the page appear. Until that step, the bar shows an empty page.
The two approaches differ most on the first visit. With client-side rendering, the user sees an empty page until the JavaScript has downloaded and run, so the first view takes longer than with server-side rendering. After that, each click is handled in the browser, and the page changes almost at once.
The next diagram compares what happens on one click after the page has loaded. Notice which steps involve the server in each case, and which part of the screen changes.
React can run in both places. In the browser, it builds and updates the page, as in the Like example. On a server, it can produce the first HTML so that the user sees content sooner, and then take over in the browser.
For a new project, the React documentation recommends starting with a framework that combines both. The examples in this course run in the browser, because that is where you learn how React updates a page.
Reading Progress
0%
On This Page
Pages That Change Without Reloading
Server-Side Rendering
Client-Side Rendering