Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting a reference to the class instance of a component

I have a typescript class that extends React.Component:

class MyComponent extends React.Component<{}, {}>
{
    constructor(props: {})
    {
        super(props);
    }

    public render()
    {
        return <span>Test</span>;
    }

    public MyMethod() {
        console.log("Working fine");
    }
}

Then there is a place where I manually have to create an instance and attach this to the DOM:

var component = MyComponent;
var element = React.createElement(component, {}, null);
ReactDOM.render(element, myDomElementContainer);

Due to architectural constraints of the system, I need to store a reference to my class instance for that component for later use, problem is that I can not find any reference to the instance of my class in the created element, it only have a reference to the class via the property type.

React.createElement is only allowing me to supply the class, and ReactDOM.render does not like a manually instantiated object.

What should I in order to instantiate a custom component, attach it to the DOM and get a reference to the instance of my component class?

like image 872
Alex Avatar asked Dec 14 '16 16:12

Alex


People also ask

How to address an instance attribute outside of a class?

To address an instance attribute outside the class, first type the reference variable, then the instance component selector (->), and only then the attribute name. The instance component selector is an arrow made up of a dash and the greater than sign.

How do I refer to a React component instance?

Referring to a Component Instance When a component is render 'ed', a React component instance is created from the passed configuration options. One can gain access to this instance and it's properties (e.g., this.props) and methods (e.g., this.setState ()) in two ways.

How do I create a new instance of a class?

To create a new instance of a class, you use the NEW operator. The example above, uses a NEW # ( ) expression on the right hand side of a value assignment. The result of the expression is the memory address of the newly created instance. This reference is then stored in the reference variable on the left-hand side of the assignment.

How do I gain access to a component instance?

One can gain access to this instance and it's properties (e.g., this.props) and methods (e.g., this.setState ()) in two ways. The first way is by using the this keyword from within a configuration function option. In the code example below all of the console.log (this) statements will refer to the component instance.


1 Answers

For my scenario I used a singleton (static variable)

export let instance = null;

class SearchDialogue extends React.Component {
    constructor(props) {
        super(props);

        instance = this;
    }
}

then when you need to reference it you can run

import {instance as searchDialogueInstance} from './SearchDialogue'

console.log(searchDialogueInstance.someFooBar);

Note

Keep in mind that this only works WELL if you're leveraging the singleton design pattern. If you are not then you could turn the static var into an array and on push new instances into the array. Careful as this could get you into trouble if you are not already familiar with potential memory pitfalls.

SearchDialogue.instances.push(this);
like image 105
Jacksonkr Avatar answered Sep 19 '22 16:09

Jacksonkr