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?
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.
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.
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.
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.
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);
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With