Author avatar

Gaurav Singhal

How to Use a Globally Defined Script Inside a React Component

Gaurav Singhal

  • Jul 31, 2020
  • 4 Min read
  • Jul 31, 2020
  • 4 Min read
Web Development
Front End Web Development
Client-side Framework


React can be used to create all kinds of web apps. The flexibility of React allows a developer to embed it into an existing web app easily. But embedding React in acurrent system means there are other libraries or scripts in use apart from React.

React uses the Virtual DOM to manipulate the DOM nodes on the web page, and most other libraries might do direct DOM manipulation. So, in this guide, you will learn how to use globally defined or externally added scripts inside a React component.

Declaring A Global Variable

A global variable is defined inside a script tag independent of the React app. This global variable can be data that needs to be prerendered on the page for SEO purposes, or it could contain the user's information.

A global variable can be defined as follows.

2window.user = {
3    id: 3,
4    name: "John Doe",

Accessing a Global Variable

The window is the global object in the browser environment. Any property attached to the window object can be accessed from any script on the web page, including the script for the React app.

Since window is a global object, the React code can also access its properties, as shown below.

1import React from "react";
3class App extends React.Component {
4  constructor(props) {
5    super(props);
7    this.state = {
8      user: window.user,
9    };
10  }
12  render() {
13    const { user } = this.state;
14    return (
15      <div>
16        <p> ID: {} </p>
17        <p> Name: {} </p>
18      </div>
19    );
20  }

You can also access the properties on the global object directly, without using the window object. But it will make the code difficult to debug as it won't be apparent where the user object is defined. Accessing any property via the window object makes it evident that it is defined and set globally.

Using External Scripts In a React Component

Now, there may be times when you need a library that does not have its own React component or might not work similarly to React—for example, jQuery.

jQuery is a popular JavaScript library, but it does not make use of the Virtual DOM; instead, it does direct DOM manipulation. So in this section, you will learn how to use jQuery in a React component.

Say you need to hide user info on a button click. To achieve this, you can use jQuery's hide() method. You usually pass a DOM selector to jQuery. But when using jQuery inside React, you need to give a ref.

A ref provides an interface to access DOM nodes that are defined in the render method of a React component. You can create a ref using the React.createRef() method. To get the actual DOM element, pass the ref to the findDOMNode() method of react-dom as you are going to manipulate the DOM node directly.

1import React from "react";
2import { findDOMNode } from "react-dom";
4class App extends React.Component {
5  constructor(props) {
6    super(props);
7    this.state = {
8      user: window.user,
9    };
10    this.domRef = React.createRef();
11  }
13  hideInfo = () => {
14    const $ = window.$;
15    const el = findDOMNode(this.domRef);
16    $(el).hide();
17  };
19  render() {
20    const { user } = this.state;
21    return (
22      <div>
23        <button onClick={this.hideInfo}>Hide Info</button>
24        <div ref={this.domRef}>
25          <p> ID: {} </p>
26          <p> Name: {} </p>
27        </div>
28      </div>
29    );
30  }


I don't advise using libraries that do direct DOM manipulation along with React. This may lead to bugs in code which may be quite challenging to track down, especially in an enterprise-level app. But sometimes you might want to use a particular library to meet specific requirements to complete your app. In such scenarios, use React refs to access the DOM node that gets mounted on the web page, and then, using the ref, you can call any method in the external library.