• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
    • Questions
    • Teachers
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

226
Views
Creating refs outside of component. Is that a bad practice?

I am working on a library which requires exporting a couple of functions for users to call upon. Those functions need access to component ref in order to add/remove classNames and auto scroll etc.

I was able to get it to work by moving my ref (created by React.createRef) outside of the component itself (NOT talking about defining it outside of the constructor but inside the component)

Here's how my code looks like (used a class component instead of functional as the hook useRef obviously can't be used outside)

import React, { PureComponent, createRef } from "react";
    import { typingEffect } from "../redux/actions/dispatch";
    import { containerRef } from "./Container";
    
    let typingRef = createRef();
    
    export async function displayTypingEffect() {
      await typingEffect();
      typingRef.current.className += " rcb-is-typing";
      containerRef.current.scrollTop = containerRef.current.scrollHeight + 700;
    }
    
    export function hideTypingEffect() {
      typingRef.current.className = "rcb-typing-container";
      containerRef.current.scrollTop = containerRef.current.scrollHeight + 700;
    }
    
    export default class Typing extends PureComponent {
      render() {
       return (
        <div ref={typingRef}>
          rest of the component code which is unnecessary for this question
        </div>
      )
    }

I am just wondering if there's a possibility of any unforeseen issues or bugs if I follow this pattern.

Thank you.

almost 3 years ago · Juan Pablo Isaza
1 answers
Answer question

0

This makes the typingRef

  1. a global variable (inside the module), and
  2. be created outside of any React life cycles

typingRef will be the same object for every instance of the Typing component, i.e. if two components are created from the Typing class, both will write to the same typingRef. Your API will provide access some DOM element, but you can not be sure which one it currently is.

typingRef is created as soon as the file is imported, before React even starts, and will live for the life time of the Javascript code, not the life time of any React component.

I think (not 100% sure) any DOM elements referenced by typingRef will be kept (at least) until typingRef gets overwritten (or the Javascript execution is ended). So if a Typing component gets unmounted, the DOM element (and everything that's connected to it) is still kept in memory. So your API will provide access to "useless" DOM elements.

almost 3 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error