🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEreact

react Documentation

LOADING ENGINE...

Installation of React

AI & DATA SCIENCE // react-installation

React can be added to a project either via a build tool like Vite for a full application, or by including script tags directly for simple pages.

Syntax

npm create vite@latest my-app -- --template react
cd my-app
npm install
npm run dev

Deep Dive Course

The recommended way to start a new React project today is a build tool like Vite, which sets up a development server with fast hot-reloading and bundles your code for production — Create React App, the older standard tool, has fallen out of favor and is no longer actively recommended for new projects. For quick experiments or embedding React into an existing non-React page without a build step, you can instead load React and ReactDOM directly via script tags from a CDN, though this approach doesn't support JSX without an in-browser transpiler like Babel, which adds noticeable overhead unsuitable for production use.

1Understanding Installation of React

The recommended way to start a new React project today is a build tool like Vite, which sets up a development server with fast hot-reloading and bundles your code for production — Create React App, the older standard tool, has fallen out of favor and is no longer actively recommended for new projects. For quick experiments or embedding React into an existing non-React page without a build step, you can instead load React and ReactDOM directly via script tags from a CDN, though this approach doesn't support JSX without an in-browser transpiler like Babel, which adds noticeable overhead unsuitable for production use.

💡

Prefer a build tool like Vite over CDN script tags for anything beyond a tiny experiment — CDN-based setups require an in-browser Babel transpiler to use JSX at all, which is slow and not suitable for a real, production application.

editor.html
# Terminal
npm create vite@latest my-app -- --template react
cd my-app
npm install
npm run dev
localhost:3000

2Practical Example

Here is a real-world application of Installation of React showing how it is used in production React code.

editor.html
<!-- Quick CDN setup, no build step -->
<script src="https://unpkg.com/react@18/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>
<script>
  const e = React.createElement;
  const root = ReactDOM.createRoot(document.getElementById('root'));
  root.render(e('h1', null, 'Hello from CDN React!'));
</script>
localhost:3000

3Best Practices

Follow these guidelines when working with Installation of React:

1. Use Vite, or a meta-framework like Next.js, to scaffold new React projects, rather than the now-outdated Create React App

2. Reserve CDN-based script-tag setups for small experiments or embedding React into an existing static page, not full applications

3. Keep React and ReactDOM versions in sync, since mismatched versions between the two packages can cause subtle runtime errors

⚠️

Tip: Prefer a build tool like Vite over CDN script tags for anything beyond a tiny experiment — CDN-based setups require an in-browser Babel transpiler to use JSX at all, which is slow and not suitable for a real, production application.

editor.html
# Terminal
npm create vite@latest my-app -- --template react
cd my-app
npm install
npm run dev
localhost:3000

Examples

Example 01Basic Usage
# Terminal
npm create vite@latest my-app -- --template react
cd my-app
npm install
npm run dev
Example 02Advanced Example
<!-- Quick CDN setup, no build step -->
<script src="https://unpkg.com/react@18/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>
<script>
  const e = React.createElement;
  const root = ReactDOM.createRoot(document.getElementById('root'));
  root.render(e('h1', null, 'Hello from CDN React!'));
</script>

Best Practices

  • Use Vite, or a meta-framework like Next.js, to scaffold new React projects, rather than the now-outdated Create React App
  • Reserve CDN-based script-tag setups for small experiments or embedding React into an existing static page, not full applications
  • Keep React and ReactDOM versions in sync, since mismatched versions between the two packages can cause subtle runtime errors

Interview Question

Why is a CDN-based script-tag setup generally unsuitable for a production React application, even though it technically works?

Hint: Think about what's missing compared to a proper build tool: bundling, JSX transpilation, optimization.

A CDN-based setup loads React as a single global script with no build step at all, meaning there's no bundler combining and optimizing your files, no minification, no code-splitting to load only what's needed, and critically, no way to use JSX directly, since JSX must be transpiled into plain JavaScript function calls before a browser can run it, which the CDN setup can only do by loading a full, slow, in-browser Babel transpiler. Real build tools like Vite handle JSX transpilation ahead of time during development and production builds, bundle and minify your code for a much smaller and faster production payload, and provide development conveniences like hot module reloading, none of which a bare CDN script setup provides, which is exactly why it's suitable only for the smallest experiments, never a real deployed application.

Exercises

MediumPractice using Installation of React in a real scenario.
View Solution
# Terminal
npm create vite@latest my-app -- --template react
cd my-app
npm install
npm run dev

Frequently Asked Questions

Why is a CDN-based script-tag setup generally unsuitable for a production React application, even though it technically works?

A CDN-based setup loads React as a single global script with no build step at all, meaning there's no bundler combining and optimizing your files, no minification, no code-splitting to load only what's needed, and critically, no way to use JSX directly, since JSX must be transpiled into plain JavaScript function calls before a browser can run it, which the CDN setup can only do by loading a full, slow, in-browser Babel transpiler. Real build tools like Vite handle JSX transpilation ahead of time during development and production builds, bundle and minify your code for a much smaller and faster production payload, and provide development conveniences like hot module reloading, none of which a bare CDN script setup provides, which is exactly why it's suitable only for the smallest experiments, never a real deployed application.

Related Functions

WhatisReactjsxcomponents