Add React to an Existing Project – React (2024)

If you want to add some interactivity to your existing project, you don’t have to rewrite it in React. Add React to your existing stack, and render interactive React components anywhere.

Note

You need to install Node.js for local development. Although you can try React online or with a simple HTML page, realistically most JavaScript tooling you’ll want to use for development requires Node.js.

Using React for an entire subroute of your existing website

Let’s say you have an existing web app at example.com built with another server technology (like Rails), and you want to implement all routes starting with example.com/some-app/ fully with React.

Here’s how we recommend to set it up:

  1. Build the React part of your app using one of the React-based frameworks.
  2. Specify /some-app as the base path in your framework’s configuration (here’s how: Next.js, Gatsby).
  3. Configure your server or a proxy so that all requests under /some-app/ are handled by your React app.

This ensures the React part of your app can benefit from the best practices baked into those frameworks.

Many React-based frameworks are full-stack and let your React app take advantage of the server. However, you can use the same approach even if you can’t or don’t want to run JavaScript on the server. In that case, serve the HTML/CSS/JS export (next export output for Next.js, default for Gatsby) at /some-app/ instead.

Using React for a part of your existing page

Let’s say you have an existing page built with another technology (either a server one like Rails, or a client one like Backbone), and you want to render interactive React components somewhere on that page. That’s a common way to integrate React—in fact, it’s how most React usage looked at Meta for many years!

You can do this in two steps:

  1. Set up a JavaScript environment that lets you use the JSX syntax, split your code into modules with the import / export syntax, and use packages (for example, React) from the npm package registry.
  2. Render your React components where you want to see them on the page.

The exact approach depends on your existing page setup, so let’s walk through some details.

Step 1: Set up a modular JavaScript environment

A modular JavaScript environment lets you write your React components in individual files, as opposed to writing all of your code in a single file. It also lets you use all the wonderful packages published by other developers on the npm registry—including React itself! How you do this depends on your existing setup:

To check whether your setup works, run this command in your project folder:

Terminal

npm install react react-dom

Then add these lines of code at the top of your main JavaScript file (it might be called index.js or main.js):

If the entire content of your page was replaced by a “Hello, world!”, everything worked! Keep reading.

Note

Integrating a modular JavaScript environment into an existing project for the first time can feel intimidating, but it’s worth it! If you get stuck, try our community resources or the Vite Chat.

Step 2: Render React components anywhere on the page

In the previous step, you put this code at the top of your main file:

import { createRoot } from 'react-dom/client';

// Clear the existing HTML content

document.body.innerHTML = '<div id="app"></div>';

// Render your React component instead

const root = createRoot(document.getElementById('app'));

root.render(<h1>Hello, world</h1>);

Of course, you don’t actually want to clear the existing HTML content!

Delete this code.

Instead, you probably want to render your React components in specific places in your HTML. Open your HTML page (or the server templates that generate it) and add a unique id attribute to any tag, for example:

<!-- ... somewhere in your html ... -->

<nav id="navigation"></nav>

<!-- ... more html ... -->

This lets you find that HTML element with document.getElementById and pass it to createRoot so that you can render your own React component inside:

Notice how the original HTML content from index.html is preserved, but your own NavigationBar React component now appears inside the <nav id="navigation"> from your HTML. Read the createRoot usage documentation to learn more about rendering React components inside an existing HTML page.

When you adopt React in an existing project, it’s common to start with small interactive components (like buttons), and then gradually keep “moving upwards” until eventually your entire page is built with React. If you ever reach that point, we recommend migrating to a React framework right after to get the most out of React.

Using React Native in an existing native mobile app

React Native can also be integrated into existing native apps incrementally. If you have an existing native app for Android (Java or Kotlin) or iOS (Objective-C or Swift), follow this guide to add a React Native screen to it.

Add React to an Existing Project – React (2024)

FAQs

Can you add React to an existing project? ›

If you want to add some interactivity to your existing project, you don't have to rewrite it in React. Add React to your existing stack, and render interactive React components anywhere.

How do I add React scripts to an existing project? ›

Add React in One Minute
  1. Step 1: Add a DOM Container to the HTML. First, open the HTML page you want to edit. ...
  2. Step 2: Add the Script Tags. Next, add three <script> tags to the HTML page right before the closing </body> tag: ...
  3. Step 3: Create a React Component. Create a file called like_button.js next to your HTML page.

How do I upgrade React on an existing project? ›

Upgrade React
  1. Step 1: Install React 18. To install the latest version, from your project folder run the following from the terminal: npm i react@latest react-dom@latest.
  2. Step 2: Use the new root API. In order to take advantage of React 18's concurrent features you'll need to use the new root API for client rendering.

How do I add React testing library to an existing project? ›

Getting Started with React Testing: React Testing Library & Jest
  1. Step 1: Install. npm install --save-dev @testing-library/react jest @types/jest prettier. ...
  2. Step 2: Add babelrc file. ...
  3. Step 3: Add Test and Run.
Dec 30, 2020

Can you add next JS to an existing React project? ›

Migrating an existing React project to Next JS can be a rewarding effort that can significantly improve your application's performance, SEO, and experience. In this Answer, we'll go through the step-by-step process of migrating an existing React project to Next JS.

Can you add Redux to an existing React project? ›

Integrating Redux into your React application can greatly enhance state management, especially for complex or large applications. This guide will walk you through the process of adding Redux to an existing React app with a simple example, focusing on managing a counter state.

How do I add a version to React? ›

In the React app, import { version } from './autobuild_version' and use as appropriate.

How do I reload a React project? ›

To do this, you can add // @refresh reset anywhere in the file you're editing. This directive is local to the file, and instructs Fast Refresh to remount components defined in that file on every edit.

How do I add Vite to an existing React project? ›

Ranga Durai
  1. Step 1: Install Vite and the React Refresh Plugin.
  2. Step 2: Set Up Vite Configuration. Create a vite.config.ts (or vite.config.js) file at your project's root:
  3. Step 3: Update package.json Scripts. Move ind.
  4. Step 4: Adjust index.html and Script References. Move (or copy) index.
  5. Step 5: Run Your Enhanced Project.
Feb 20, 2024

How do I add a library to React project? ›

You can install a particular version of the library by running npm install <library-name>@<version-number> , for example: npm install @react-native-community/netinfo@^2.0.0 .

How do I import an existing React project into Visual Studio code? ›

You can directly open the folder which contains the existing React application(File > Open > Folder…), but only four npm tasks are available(right-click the package. json file > Npm): npm run-script start, npm run-script build, npm run-script test and npm run-script eject. The . esproj file(project file) and the .

How do I open an existing React project? ›

How do I run an existing react app? To run an existing react app, clone or download the app's repository, navigate to the app's directory in the terminal, and run npm install to install dependencies. After that, execute npm start to run the app.

How do I install React in my project? ›

How To Install React on Windows
  1. Step 1: Install Node. js and npm.
  2. Step 2: Install Create React App.
  3. Step 3: Create a New React Project.
  4. Step 4: Go To the Project Directory and Start the Development Server.
Jun 6, 2023

How do I add React icons to my project? ›

[React] - How to import icons into React
  1. Find the specific icon you want to use on the icon library's website or documentation. Each icon should have a unique class name.
  2. Add the icon to your component using the library's icon component or HTML tag (depending on the library).

Do I have to install react app again every time I start a new project? ›

There are plenty of other boilerplates you can download but this one is the most popular. If you are concerned with the size of it, you do not need to run create-react-app every time. You can make a react project yourself quite easily and by doing so you have much more control and understanding of your project.

How do I import a React component from another project? ›

You can move a component in three steps:
  1. Make a new JS file to put the components in.
  2. Export your function component from that file (using either default or named exports).
  3. Import it in the file where you'll use the component (using the corresponding technique for importing default or named exports).

Top Articles
The Apple Logo: A Bite of Innovation and Mystery
Leerverkäufe auf Währungen und Gold wieder steuerpflichtig
Section 4Rs Dodger Stadium
Repentance (2 Corinthians 7:10) – West Palm Beach church of Christ
Manhattan Prep Lsat Forum
Summit County Juvenile Court
FFXIV Immortal Flames Hunting Log Guide
Cad Calls Meriden Ct
Owatc Canvas
Self-guided tour (for students) – Teaching & Learning Support
Minn Kota Paws
Mndot Road Closures
Missing 2023 Showtimes Near Lucas Cinemas Albertville
MindWare : Customer Reviews : Hocus Pocus Magic Show Kit
Flights To Frankfort Kentucky
Blackwolf Run Pro Shop
Accuweather Mold Count
Account Suspended
Sulfur - Element information, properties and uses
Invitation Homes plans to spend $1 billion buying houses in an already overheated market. Here's its presentation to investors setting out its playbook.
Bennington County Criminal Court Calendar
What Are The Symptoms Of A Bad Solenoid Pack E4od?
E32 Ultipro Desktop Version
Defending The Broken Isles
Chicago Based Pizza Chain Familiarly
Catchvideo Chrome Extension
Bfsfcu Truecar
The Fabelmans Showtimes Near Baton Rouge
Guinness World Record For Longest Imessage
Ryujinx Firmware 15
Perry Inhofe Mansion
2430 Research Parkway
Best Workers Compensation Lawyer Hill & Moin
Best Restaurants In Blacksburg
Mugshots Journal Star
Sun Tracker Pontoon Wiring Diagram
Payrollservers.us Webclock
9:00 A.m. Cdt
705 Us 74 Bus Rockingham Nc
CrossFit 101
Noga Funeral Home Obituaries
Adams-Buggs Funeral Services Obituaries
Enter The Gungeon Gunther
CPM Homework Help
Motorcycle For Sale In Deep East Texas By Owner
Greg Steube Height
Diamond Spikes Worth Aj
Lagrone Funeral Chapel & Crematory Obituaries
Latest Posts
Article information

Author: Rubie Ullrich

Last Updated:

Views: 5855

Rating: 4.1 / 5 (72 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Rubie Ullrich

Birthday: 1998-02-02

Address: 743 Stoltenberg Center, Genovevaville, NJ 59925-3119

Phone: +2202978377583

Job: Administration Engineer

Hobby: Surfing, Sailing, Listening to music, Web surfing, Kitesurfing, Geocaching, Backpacking

Introduction: My name is Rubie Ullrich, I am a enthusiastic, perfect, tender, vivacious, talented, famous, delightful person who loves writing and wants to share my knowledge and understanding with you.