Search code examples
reactjsclassinstancereact-dom

where is instance of class created


We are shown the following code:

class HelloComponent extends React.Component{
   render() {
     return(
            <h1>Hello</h1>
     );
   }
}

ReactDom.render(
    <HelloComponent />, document.getElementById('container)
);

Since HelloComponent is a class does ReactDOM.render create an instance?


Solution

  • The ReactDOM.render() method returns a reference to the component.

    This mean you can do something like:

    const helloComponent = ReactDom.render(
        <HelloComponent />, document.getElementById('container)
    );
    

    And have a reference to the component you can use for other purposes if you want.

    NOTE: This does not work for stateless/functional components.