1

Up until now I have been using create-react-app to build React applications and components.

However, I have a project that I'm working on which was built in node using pure JS for Dom manipulation and I wanted to add react to one page only (the cart page).

All tutorials I had watched assume you are starting project from scratch and I can't seem to figure out how to add React to just a single part of my project.

Can someone point me in the right direction?

bcperth
  • 2,191
  • 1
  • 10
  • 16
tito.300
  • 976
  • 1
  • 9
  • 22

2 Answers2

2

I would recommend you start here: https://reactjs.org/docs/rendering-elements.html

The React Docs actually also point at this tutorial for a non 'create-react-app' tutorial: https://blog.usejournal.com/creating-a-react-app-from-scratch-f3c693b84658

This is the React Docs for rendering elements. The TLDR version:

In your HTML file, where you want your React component to go, create an empty div and give it a unique name (i.e. 'app' or 'react-component')

Then create your component, etc. and have ReactDOM render on the unique id name.

To get it to render, in your node app, point it at the build path, typically bundle.js

pinkwaffles
  • 463
  • 4
  • 12
  • do I store the react modules/component in an adjacent and separate folder than public and views? Can you elaborate on what you mean by 'To get it to render, in your node app, point it at the build path'? note that I am not using webpack in the project yet – tito.300 Oct 23 '18 at 22:56
  • Ah - well if you're looking to avoid webpack, and i suppose also babel, look at this super lightweight React example: [Inline React](https://medium.com/@clmyles/react-without-npm-babel-or-webpack-1e9a6049714) I recommend these articles because they are much meatier than say - a StackOverflow answer. – pinkwaffles Oct 23 '18 at 23:08
0

I have got this working and managed to use React components (written in JSX) for specific parts of my custom JavaScript app (bundled via Webpack). You basically need three things.

1) Your custom JavaScript app

// MyApp.js
import { renderMyReactComponent } from "./MyReactComponent.jsx";

class MyApp {

    // Call this when you want to show your React component
    showMyReactComponent() {

        // Create an empty div and append it to the DOM
        const domElem = document.createElement("div");
        domElem.classList.add("my-react-component");  
        document.append(domElem);

        // Render your React component into the div
        renderMyReactComponent(domElem);
    }
}

2.) Your React component

// MyReactComponent.jsx
import React from "react";
import ReactDOM from "react-dom";

class MyReactComponent extends React.Component {

    render() {
        // JSX, woah!
        return <h2>My React Component</h2>
    }
}

// A way to render your React component into a specific DOM element
export const renderMyReactComponent = (domElem) => {
    // NB: This syntax works for React 16.
    // React 18 requires a slightly different syntax.
    ReactDOM.render(
        <MyReactComponent />,
        domElem
    );
}

3.) A way to parse jsx files and build the app

I use Webpack and amended my existing Webpack configuration based on this article: https://medium.com/@JedaiSaboteur/creating-a-react-app-from-scratch-f3c693b84658 (The official React documentation also points at this tutorial)


Useful Articles

PS: Note that at the time of writing this answer, the above articles already use React 18 for their examples, whereas my above example still uses React 16; so the syntax regarding ReactDOM is a little different. But the idea is the same.

Ben
  • 15,938
  • 19
  • 92
  • 138
  • You can actually integrate React to an existing app much easier. https://reactjs.org/docs/add-react-to-a-website.html – Dilshan Jun 10 '22 at 03:20
  • @Dilshan Depends on what you mean by "much easier"? I assume you are referring to the approach using just script tags? That's possible, sure, but it doesn't scale well. Once you start wanting to use jsx in a production app, you'd have to use Babel anyway. So IMO using Webpack is the way to go. – Ben Jun 10 '22 at 03:32
  • In the case of OP he just wanted to integrate it to his existing javascript in his view layer just for the cart. In that case it is much easier to just use compiled library code & babel code. You can still use JSX without bundlers. [Just like this](https://reactjs.org/docs/add-react-to-a-website.html#optional-try-react-with-jsx) – Dilshan Jun 10 '22 at 06:28
  • @Dilshan Yes, but the docs for that section state: *"This approach is fine for learning and creating simple demos. However, it makes your website slow and isn’t suitable for production."* The alternative approach I presented is suitable for production. – Ben Jun 10 '22 at 08:23